
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
]
]
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
- How to use or operator in laravel
- On delete set foreign id column value null using migration in laravel 8
- Automatically remove records using Prunable trait in Laravel
- How to call Laravel route in jQuery
- The POST method is not supported for this route. Supported methods: PUT.
- How to add columns in existing table using migration in laravel
- Insert dummy data in users table Laravel
- Method Illuminate\Http\Request::validated does not exist
- Laravel create default admin user
- Always load the relationship data with eager loading in Laravel
- How to get selected categories on edit record with Select2
- How to get tomorrow and yesterday date in laravel
- Trying to get property 'title' of non-object
- How to create belongstomany relation using custom name on custom pivot table
- How to get last year records count with month wise in Laravel
- How to add unique records in pivot columns of Laravel pivot table
- Comment .env file in laravel
- Extra Filter Query on Relationships in Laravel
- How to show data by ID in laravel?
- How to remove package from laravel
- How to include header file in laravel
- Class 'App\Providers\Auth' not found
- Array to string conversion laravel blade
- How to check relationship is loaded or not in Laravel
- How to pass data to route in laravel?