Тёмный
No video :(

(62) What is Query Scope | Global Scopes in Laravel | Why we use Global scope in Laravel 

Career Development Lab
Подписаться 9 тыс.
Просмотров 1,1 тыс.
50% 1

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
Let's create a new class under app/Scopes/HasActiveScope.php file with following contents:
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.
github.com/Had...
If you are facing any problem during development, you can post your queries free of cost at solutions.cdlc... so our team will respond to you within 24 hours.
If you need paid help from us, please visit solutions.cdlc... to process a contract with us.
* Contact us: contact@cdlcell.com
* Our website: cdlcell.com
* My Twitter: / hadayatniazi3
* My FB: / hadayatniaziofficial
Follow us on LinkedIn: / cdlcell
Follow us on Tiktok: / cdlcell
Join us on Instagram: / cdlcell
Follow us on Facebook: / cdlcellpk
Join our Laravel Developers whatsapp group: chat.whatsapp....
#php
#phplaravel
#laravel
#webdevelopment
#cdl
#career_development_lab
#hadayatniazi
#coding
#technology

Опубликовано:

 

23 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 2   
@blmdev4472
@blmdev4472 Год назад
how add a variable to global scope ??
@LaravelLover
@LaravelLover Год назад
I think there is no need to add variable in global scope, basically it's a generic condition. If you want to apply scope where you want to add variable then create local scope. Watch my next video it will resolve your issue.
Далее
Laravel Basics - Scopes
16:36
Просмотров 5 тыс.
Laravel Global Model Scopes using PHP Attributes
3:04
Просмотров 2,4 тыс.