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
- Property [user] does not exist on this collection instance
- Undefined property: stdClass::$title
- Attempt to read property "avatar" on null in Laravel
- Create project table with model and migration
- Laravel append URI in route
- How to check records exist in loaded relationship in Laravel blade view
- Validation for multiple forms on same page in laravel
- Setup laravel project with docker
- How to Access Array in blade laravel
- How to get route method name in Laravel
- Laravel migration add foreign key to existing table
- How to create new user without form submission in laravel
- Create project factory and seed data in laravel
- Permission denied error while creating storage link in Laravel
- Input file with max size validation in laravel
- Use withCount() to get total number of records with relationship
- How to insert dynamic value to additional column in pivot table in laravel
- Use of undefined constant laravel
- How to add script on specific view file in laravel while extending layout
- How to upload files to amazon s3 bucket using Laravel
- How to get only time from created_at in laravel
- How to add dynamic page title in Laravel view
- Calculate age from date of birth in Laravel
- Remove public from url in laravel project
- How to customize pagination view in laravel