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
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- Datetime field in Laravel migration
- How to check records exist in loaded relationship in Laravel blade view
- How to display pivot table column value in laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- PhpMyAdmin - Error The mysqli extension is missing
- How to insert value to additional columns in pivot table in laravel
- Post model with title and body in laravel 8
- How to create belongstomany relation using custom name on custom pivot table
- How to check if user has created any post or not in laravel
- Illuminate\Database\QueryException could not find driver
- Laravel 9 pagination with search filter
- How to add active class to menu item in laravel
- How to use bootstrap pagination in laravel 8
- Get previous date data in laravel
- Laravel hasmany select not working
- Retain selected value of select box in Laravel
- Laravel URL validation not working
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Laravel get count with where condition
- Class "App\Http\Controllers\Auth\Verified" not found
- How to check record exist or not in relationship table
- Extra Filter Query on Relationships in Laravel
- Get comma separated email from input array