Global scope in Laravel with example
Created at 11-Jan-2022 ,
By samar
Global scope in Laravel with example
In this tutorial, we will try to find the solution to "Global scope in Laravel with example" through programming.
Here we have some global scope examples in Laravel. Global scopes provide a convenient, easy way to make sure every query for a given model receives certain constraints. In a simple way, we can say that we can add some filter to every query for a specific model.-
Use of global scope in Laravel
//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); } } //app\Models\User.php <?php namespace App; use App\Scopes\ActiveScope; use Illuminate\Database\Eloquent\Model; class User extends Model { protected static function booted() { static::addGlobalScope(new ActiveScope); } } //routes\web.php Route::get('/get-active-users-using-global-scope', function(){ DB::enableQueryLog(); $users = App\Models\User::all(); dd(DB::getQueryLog()); });
Output:
^ array:1 [▼
0 => array:3 [▼
"query" => "select * from `users` where `is_active` = ?"
"bindings" => array:1 [▶]
"time" => 4.57
]
]
Related Queries
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Laravel 10 Breeze Authentication Example
- Run artisan command to generate key in laravel
- Drop foreign key column in Laravel using migration
- Pagination in laravel
- Get laravel version
- Rendering HTML from database table to view in Laravel
- Laravel onclick function not working
- How to randomly get the user id from users table in laravel
- How to pass two variables in HREF in laravel
- How to Access Array in blade laravel
- Cast Array to an Object in Controller and then pass to view in laravel
- Laravel route parameter
- How to Get records between two dates in Laravel
- Get domain name in laravel
- Print query in laravel
- If condition in Laravel 9
- How to create static page in Laravel
- Setup laravel project with docker
- How to use bootstrap pagination in laravel 8
- Laravel change date format
- Permission denied error while creating storage link in Laravel
- Laravel 5.4 save data to database
- How to display order by null last in laravel
- Automatically remove records using Prunable trait in Laravel
- Delete file from amazon s3 bucket using Laravel