filmov
tv
(62) What is Query Scope | Global Scopes in Laravel | Why we use Global scope in Laravel
Показать описание
How to Create and Use Query Scope in Laravel Eloquent
Today, i would like to share example of how to use laravel eloquent model scope. i will show you how to create model eloquent query scope in laravel 6 and how it will easy for you. i will guide you to create custom query function in model eloquent using scope in laravel 6, laravel 7 and laravel 8 app.
Sometime, we are working model query like get todays records, get active records, get banned users etc. At this time we always use where condition everywhere on controller file. i think where condition is right it is not wrong, but you have to write if again and again with some login like current date and etc. If you create laravel model eloquent scope then you don't have to write same logic with where condition again and again. You can just use function like "latest()". You can easily re-use that scope on your model query.
Laravel8 Global Scope in Model
Writing Global Scopes
public function apply(Builder $builder, Model $model)
{
$builder where('active', true);
}
Applying Global Scopes
To assign a global scope to a model, you should override the model's booted method and invoke the model's addGlobalScope method. The addGlobalScope method accepts an instance of your scope as its only argument:
// using seperate scope class
static::addGlobalScope(new HasActiveScope);
// you can do the same thing using anonymous function
// let's add another scope using anonymous function
static::addGlobalScope('delete', function (Builder $builder) {
$builder where('deleted', false);
});
How to use global scope?
Once your global scopes are added into booted function you do not need to worry about using them. These global scope are automatically applied to your model queries. Let's check out some examples:
Remove Scope From Query
In some cases, you might not want your global filter to apply. For example, if you want to fetch all users weather they are active or not. In that case you can remove your applied scope using following example:
$users = User::withoutGlobalScope(new HasActiveScope) all();
Laravel step by step guide
Laravel full series
Laravel scratch to advance series
Laravel zero to hero course
Laravel guide
The project is stored in the given below directory.
#php
#phplaravel
#laravel
#webdevelopment
#cdl
#career_development_lab
#hadayatniazi
#coding
#technology
Today, i would like to share example of how to use laravel eloquent model scope. i will show you how to create model eloquent query scope in laravel 6 and how it will easy for you. i will guide you to create custom query function in model eloquent using scope in laravel 6, laravel 7 and laravel 8 app.
Sometime, we are working model query like get todays records, get active records, get banned users etc. At this time we always use where condition everywhere on controller file. i think where condition is right it is not wrong, but you have to write if again and again with some login like current date and etc. If you create laravel model eloquent scope then you don't have to write same logic with where condition again and again. You can just use function like "latest()". You can easily re-use that scope on your model query.
Laravel8 Global Scope in Model
Writing Global Scopes
public function apply(Builder $builder, Model $model)
{
$builder where('active', true);
}
Applying Global Scopes
To assign a global scope to a model, you should override the model's booted method and invoke the model's addGlobalScope method. The addGlobalScope method accepts an instance of your scope as its only argument:
// using seperate scope class
static::addGlobalScope(new HasActiveScope);
// you can do the same thing using anonymous function
// let's add another scope using anonymous function
static::addGlobalScope('delete', function (Builder $builder) {
$builder where('deleted', false);
});
How to use global scope?
Once your global scopes are added into booted function you do not need to worry about using them. These global scope are automatically applied to your model queries. Let's check out some examples:
Remove Scope From Query
In some cases, you might not want your global filter to apply. For example, if you want to fetch all users weather they are active or not. In that case you can remove your applied scope using following example:
$users = User::withoutGlobalScope(new HasActiveScope) all();
Laravel step by step guide
Laravel full series
Laravel scratch to advance series
Laravel zero to hero course
Laravel guide
The project is stored in the given below directory.
#php
#phplaravel
#laravel
#webdevelopment
#cdl
#career_development_lab
#hadayatniazi
#coding
#technology