<?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'));
});
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('is_active');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class UserFactory extends Factory {
/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'name' => $this->faker->name(),
'is_active' => $this->faker->numberBetween(1, 10),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
/**
* Indicate that the model's email address should be unverified.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function unverified() {
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
}
C:\xampp\htdocs\reset\app\Models\User.php
<?php
namespace App\Models;
use App\Scopes\ActiveScope;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable {
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name', 'email', 'password', 'is_active',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
protected static function boot() {
parent::boot();
static::addGlobalScope(new ActiveScope());
}
}
C:\xampp\htdocs\reset\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);
}
}
C:\xampp\htdocs\reset\app\Admin.php
<?php
namespace App;
use App\Scopes\ActiveScope;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model {
protected $fillable = [
'name', 'email', 'password', 'is_active',
];
protected static function boot() {
parent::boot();
return static::addGlobalScope(new ActiveScope());
}
}
Sử dụng withoutGlobalScope
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();
// $users = User::withoutGlobalScope('App\Scopes\ActiveScope')->get();
return view('welcome')->with(compact('users'));
});
Laravel Global Scope Tutorial Example
By Hardik Savani December 28, 2019 Category : LaravelPlayUnmuteLoaded: 1.17%FullscreenHi 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.
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
<?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 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
<?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 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
<?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 you can use that with your controller file.
You will get only active records using bellow queries: