Laravel Global Scope Tutorial Example (ok)

https://www.itsolutionstuff.com/post/laravel-global-scope-tutorial-exampleexample.html

C:\xampp\htdocs\reset\routes\web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Models\User;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('my-components', function () {
  $users = User::select('*')->get();
  return view('welcome')->with(compact('users'));
});

C:\xampp\htdocs\reset\resources\views\welcome.blade.php

C:\xampp\htdocs\reset\database\migrations\2014_10_12_000000_create_users_table.php

C:\xampp\htdocs\reset\database\factories\UserFactory.php

C:\xampp\htdocs\reset\app\Models\User.php

C:\xampp\htdocs\reset\app\Scopes\ActiveScope.php

C:\xampp\htdocs\reset\app\Admin.php

Sử dụng withoutGlobalScope

C:\xampp\htdocs\reset\routes\web.php

Laravel Global Scope Tutorial Example

By Hardik Savani December 28, 2019 Category : LaravelPlayUnmuteLoaded: 1.17%FullscreenVDO.AIHi Guys,

In this tutorial, i would like to show you how to define global scope in laravel and how to use global scope in laravel 6 application. i will give you demo to how to create global scope in laravel and how to remove global scope in laravel 6, laravel 7, laravel 8 and laravel 9.

Global Scope is a very important concept of laravel 6. using Global Scope you can reuse same eloquent condition in laravel application. i also written tutorial of how to create local scope in laravel 6. Local scope it only for single model. But you define global scope then you can use with all model.

If you want to see how works local scope then you can simply read this tutorial: https://www.itsolutionstuff.com/post/how-to-create-and-use-query-scope-in-laravel-eloquentexample.html.

In this example we will create ActiveScope for getting only active records from model. How to use with multiple models same scope. You can check it step by step bellow:

Create Global Scope File

In this step, we will create new ActiveScope global scope class as like bellow:

app\Scopes\ActiveScope.php

Define Global Scope in User Model

Here, we will add today scope in our user model. So when we query in controller then we will use that scope in laravel model.

app/User.php

Define Global Scope in Admin Model

Here, we will add today scope in our admin model. So when we query in controller then we will use that scope in laravel model.

app/Admin.php

Get Active Records:

Now you can see how you can use that with your controller file.

You will get only active records using bellow queries:

Get All Records:

You will get only add records using bellow queries from users and admins table:

Read Also: How to Create and Use Query Scope in Laravel Eloquent

I hope it can help you...

Last updated

Was this helpful?