I have a model accounts in Laravel that uses Softdeletes.
Beside that, I want another column called active, that behaves kinda the same. If its sets to XXX then all Account::
calls shouldnt include them in the return. Until I set the active value to YYY, after this, they should be included.
Is there any way to do this?
I want to set the XXX and YYY values by my own. So I could say return only if active = 1 or active = whatEverIWant
Edit: I know I could do that by checking the value at every call, but I already use the accounts at a lot of places and dont want to add it everywhere
Can be approached in two ways
Option 1: Global Scope
You can define a global scope on the model, which returns records only with active set to 1
Now all the queries on the model will have the constraint defined in global scope.
When you do want to retrieve records irrespective of whether active is set to 1 not, you can call it with
withoutGlobalScopes()
orwithoutGlobalScope('active')
- will remove the constraint defined in global scope.Option 2: Local Scope
You can define a local scope on the model class for each of active and inactive states
Then when you want to filter records which are active or inactive
Laravel softdeletes also uses scopes behind the scenes.
Laravel docs: https://laravel.com/docs/master/eloquent#query-scopes