Using Scope in Laravel

Mantan Programmer
Towards Dev
Published in
3 min readApr 28, 2022

--

Using Scope in Laravel
Hello, how are you all friends, I wish you good health and success always. This time we will discuss Scope in Laravel, this is very useful for friends when creating projects that have many filters.

Simply put, that scope is a query that we have created and reused in other modules, so by using a scope we don’t need to write the same query again.

the point is laravel scope is to make it easier for us not to write the same query over and over again..

There are actually 2 types of Laravel scopes, namely Global Scope and Local Scope.

Global Scope

For global scope / Global Where we have discussed it before, please visit the article here: Creating a Global Where in Eloquent Laravel..

So with global scope, we only need to write the query once in the model, then automatically the query will always be called when we use the model, without having to write the query again.

Local Scope

Local Scope is actually the same as global scope, only we need to call the initial scope back in the query..

For example, for example, we have several methods that pull customer data with conditions that only have an active status.

// method index 
public function index()
{
$data = M_customer::where('is_active', 1)->get(); // menarik data customer yg status nya aktif
}
// method show
public function show()
{
$data = M_customer::where('is_active', 1)->get(); // menarik data customer yg status nya aktif
}

In the 2 methods above, in the query we are both pulling customer data whose status is active only.

Those are just 2 methods, what if it turns out that in another controller we will only pull data from active customers?? Then we will write the query where is_active=1 continuously and repeatedly..

Of course this is very inefficient, why? because we write the same query over and over again..

Just imagine what if one day the condition changes, which was initially is_active=1 then changed to is_active=’active’.. So we need to change all the queries on each method and on each controller..

The solution is to use a local scope..

How to write Local Scope
you can simply add a local scope to the M_customer model as follows:

....
public function scopeActive($e)
{
return $e->where('is_active', 1);
}
....

Then in the query you just need to call it like this:

....
// method index
public function index()
{
$data = M_customer::active()->get(); // menarik data customer yg status nya aktif
}
// method show
public function show()
{
$data = M_customer::active()->get(); // menarik data customer yg status nya aktif
}
....

It looks more simple and concise, which initially always wrote ->where(‘is_active’,1).. now only needs to write ->active() only.

And even if the query changes later, we only need to change the function scope.

Local Scope writing requirements

Local Scope writing requirements are:

  1. Beginning with the word scope in the function name.
....
public function scopeActive($e){
....

2. The function name is written in Camel Case style

3. When calling the scope in the query, we no longer need to write the word “scope”.

How to bring Parameters into Scope
Suppose we want to make the value in the scope dynamic, we can pass one or more parameters to it..

Take for example:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
/**
* Scope a query to only include users of a given type.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param mixed $type
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeOfType($query, $type)
{
return $query->where('type', $type);
}
}
....
$users = User::ofType('admin')->get();
....

That’s the tutorial that I can convey, hopefully it’s useful

Thank you…

Source: Using Scope in Laravel

--

--