I need to set a default where condition in my models.
So actually i have to set it in my all select queries.
Query like:
->where('status','active')
You can use laravel scope (local scope or global scope )in model:
Global scope Example :
In Model.php:
protected static function boot()
{
parent::boot();
static::addGlobalScope('status', function (Builder $builder) {
$builder->where('status', 'active');
});
}
Local Scope Example :
In Model.php
public function scopeIsActive($query)
{
return $query->where('status', 'active');
}
in controller :
Model::isActive()->get();
source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With