Vivek Mistry šŸ‘‹

I’m a Certified Senior Laravel Developer with 6+ years of experience , specializing in building robust APIs and admin panels, frontend templates converting them into fully functional web applications.

Book A Call
  • 09 Apr, 2025
  • 294 Views
  • Eloquent ORM > Query Scope

Query Scopes in Laravel

Scopes make it easy to reuse parts of your database queries in your models. To create one, just add the word "scope" to the beginning of a method in your model.

How to define Scope?

Below example of Currency model. Now, you want to fetch the active currency at front-end and then no need to write every time with "where" conditions. You can define the scope method and reuse everytime along with the Model.

class Currency extends Model
{
    use HasUlids, SoftDeletes;


    protected $table = 'currencies';


    public $fillable = [
        'title',
        'symbol',
        'country',
        'is_active',
    ];


    public function scopeIsActive($query)
    {
        return $query->whereIsActive(1);
    }
}

How to Use?

$currency = Currency::isActive()->orderBy('created_at')->get();

More Info about Query Scope visit,

https://laravel.com/docs/5.0/eloquent#query-scopes

Share: