Wednesday, February 26, 2020

How Global Scope works in Laravel?

How Global Scope works in Laravel?

Hi Buddies,

In this tutorial, I want to show you how to define global reach in Laravel and how to use global reach in Laravel 6 application. I'll show you how to create a global reach in Laravel and how to remove a global reach in Laravel.

Global Scope is a very important concept of Laravel 6. The use of Global Scope can reuse the same eloquent condition when using Laravel. I also wrote a tutorial on how to create a local area in Laravel 6. The local area only applies to a single model. However, if you define the global reach, you can use it with all models.


In this example, we create ActiveScope to retrieve only active records of the model. Use with multiple models in the same range. You can check this step by step:

Create Global Scope File


In this step, we create a new global ActiveScope range class as follows:

app\Scopes\ActiveScope.php
<?php


  


namespace App\Scopes;


  


use Illuminate\Database\Eloquent\Builder;


use Illuminate\Database\Eloquent\Model;


use Illuminate\Database\Eloquent\Scope;


  


class ActiveScope implements Scope


{


    /**


     * Apply the scope to a given Eloquent query builder.


     *


     * @param  \Illuminate\Database\Eloquent\Builder  $builder


     * @param  \Illuminate\Database\Eloquent\Model  $model


     * @return void


     */


    public function apply(Builder $builder, Model $model)


    {


        $builder->where('is_active', '=', 1);


    }


}


Define Global Scope in User Model


Here we are going to expand our user model today. So if we check the controller, we will use this area in the Laravel model.

app/User.php
<?php


  


namespace App;


  


use Illuminate\Database\Eloquent\Model;


use App\Scopes\ActiveScope;


  


class User extends Model


{


    protected $fillable = [


        'name','email','password','is_active',


    ];


  


    protected static function boot()


    {


        parent::boot();


  


        static::addGlobalScope(new ActiveScope);


    }


}


Define Global Scope in Admin Model


Here we will expand our admin model today. So if we check the controller, we will use this area in the Laravel model.

app/Admin.php
<?php


  


namespace App;


  


use Illuminate\Database\Eloquent\Model;


use App\Scopes\ActiveScope;


  


class Admin extends Model


{


    protected $fillable = [


        'name','email','password','is_active',


    ];


  


    protected static function boot()


    {


        parent::boot();


  


        return static::addGlobalScope(new ActiveScope);


    }


}


Get Active Records:


Now you can see how to use that with your driver file.

You will only receive active data records with the following queries:


$users = User::select('*')->get();


  


$admins = Admin::select('*')->get();



Get All Records:


You only add records with the following queries from the user and administrator table:

$users = User::select('*')->withoutGlobalScope(ActiveScope::class)->get();


  


$admins = Admin::select('*')->withoutGlobalScope(ActiveScope::class)->get();


I hope This Could help you ...

0 comments:

Post a Comment

Please don't enter any spam link in the comment box.