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
class Account extends Model
{
protected static function booted()
{
static::addGlobalScope('active', function (Builder $builder) {
$builder->where('active', 1);
});
}
// rest of the class code
}
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()
or withoutGlobalScope('active')
- will remove the constraint defined in global scope.
$accounts = Account::withoutGlobalScopes()->get();
//OR
$accounts = Account::withoutGlobalScope('active');
Option 2: Local Scope
You can define a local scope on the model class for each of active and inactive states
class Account extends Model
{
public function scopeActive($query)
{
$query->where('active', 1);
}
public function scopeInactive($query)
{
$query->where('active', '<>', 1);
}
}
Then when you want to filter records which are active or inactive
$activeAccounts = Account::active()->get();
$inactiveAccounts = Account::inactive()->get();
$accounts = Account::get(); //to get all records irrespective of whether active or inactive
Laravel softdeletes also uses scopes behind the scenes.
Laravel docs: https://laravel.com/docs/master/eloquent#query-scopes