Understanding Scopes in Laravel

When you’re starting with Laravel, writing database queries using where conditions is very common. But as your project grows, you may find yourself repeating the same query logic again and again.

That’s where scopes come in.

In this blog, we’ll break down what scopes are, why they’re useful, and how you can use them in real-world applications — all in a simple and beginner-friendly way.


🔹 What is a Scope?

A scope in Laravel is a way to define reusable query logic inside your model.

Instead of writing the same where conditions multiple times, you can wrap them inside a method and reuse them whenever needed.


🔹 Why Use Scopes?

Without scopes, your code might look like this:

User::where('status', 'active')
    ->where('age', '>=', 18)
    ->get();

Now imagine writing this in multiple places — it becomes repetitive and harder to maintain.

With scopes, you can write:

User::active()->adults()->get();

Much cleaner, right?


🔹 How to Create a Scope

Scopes are defined inside your model.

Example:

class User extends Model
{
    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }
}

How to Use It:

User::active()->get();

👉 Notice:

  • The method name starts with scope
  • But when calling it, you just use active()

🔹 Real-World Use Cases

Let’s look at some practical examples.


✅ 1. Filtering Active Users

public function scopeActive($query)
{
    return $query->where('status', 'active');
}

Usage:

User::active()->get();

✅ 2. Verified Users

public function scopeVerified($query)
{
    return $query->whereNotNull('email_verified_at');
}

Usage:

User::verified()->get();

✅ 3. Filtering Orders

class Order extends Model
{
    public function scopeCompleted($query)
    {
        return $query->where('status', 'completed');
    }

    public function scopeRecent($query)
    {
        return $query->where('created_at', '>=', now()->subDays(7));
    }
}

Usage:

Order::completed()->recent()->get();

✅ 4. Dynamic Scope (With Parameters)

public function scopeRole($query, $role)
{
    return $query->where('role', $role);
}

Usage:

User::role('admin')->get();

🔹 What Are Global Scopes?

Global scopes are applied automatically to all queries of a model.

Example:

protected static function booted()
{
    static::addGlobalScope('active', function ($query) {
        $query->where('status', 'active');
    });
}

Now every query will only return active users.


🔹 Key Benefits

  • ✅ Reduces repeated code
  • ✅ Makes queries easy to read
  • ✅ Keeps logic in one place
  • ✅ Easier to maintain and update

🔹 Important Tips for Beginners

  • Always prefix scope methods with scope
  • Don’t use scope when calling them
  • Keep scopes simple and focused
  • Use meaningful names like active, recent, verified

🔹 Final Thoughts

Scopes are a powerful feature that help you write cleaner and more maintainable code in Laravel.

Instead of writing long and repetitive query conditions, you can define reusable filters and apply them with simple method calls.

As your application grows, using scopes will save you time and make your code much easier to understand.

In short: Scope is used to avoid repeating multiple query conditions by wrapping them inside a reusable function.

Happy Coding!