Laravel 6 Global Scope Tutorial Example

Today, We want to share with you Laravel 6 Global Scope Tutorial Example.In this post we will show you Lesson 6 Global Scopes and Traits Eloquent by Example, hear for how to create global scope in laravel 6 we will give you demo and example for implement.In this post, we will learn about laravel 6 global scope where with an example.

Laravel 6 Global Scope Tutorial Example

There are the Following The simple About laravel 6 global scope with parameter Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 6 Eloquent Global Scope how-to, so the laravel 6 global scope with parameter is used for this example is following below.

Read Also:  PayPal Subscriptions(recurring payments) Payment Gateway Integration in PHP Source Code Download

Step 1: Create Global Scope File

app\Scopes\ConfirmScope.php

<?php
  
namespace App\Scopes;
  
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
  
class ConfirmScope 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_available', '=', 1);
    }
}

Step 2: Laravel Define Global Scope in Member Model

app/Member.php

<?php
  
namespace App;
  
use Illuminate\Database\Eloquent\Model;
use App\Scopes\ConfirmScope;
  
class Member extends Model
{
    protected $fillable = [
        'name','email','password','is_available',
    ];
  
    protected static function boot()
    {
        parent::boot();
  
        static::addGlobalScope(new ConfirmScope);
    }
}

Step 3: Define Global Scope in Leader Model

app/Leader.php

<?php
  
namespace App;
  
use Illuminate\Database\Eloquent\Model;
use App\Scopes\ConfirmScope;
  
class Leader extends Model
{
    protected $fillable = [
        'name','email','password','is_available',
    ];
  
    protected static function boot()
    {
        parent::boot();
  
        return static::addGlobalScope(new ConfirmScope);
    }
}

Step 4: Retrives Active Records:

$members = Member::select('*')->get();
  
$leaders = Leader::select('*')->get();

Step 5: Fetch All Records:

add records using bellow queries from members and leaders table

Read Also:  Laravel 6 Eloquent Global Scope Tutorial Example

$members = Member::select('*')->withoutGlobalScope(ConfirmScope::class)->get();
  
$leaders = Leader::select('*')->withoutGlobalScope(ConfirmScope::class)->get();

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about dynamic global scope laravel 6.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.