
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
- Class 'App\Http\Controllers\User' not found
- How to get specific columns using Laravel eloquent methods
- Trying to access array offset on value of type null error in laravel
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- Always load the relationship data with eager loading in Laravel
- How to return error message from controller to view in laravel
- Laravel csrf token mismatch for ajax POST Request
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- External link not working in laravel blade
- How to restore deleted records in laravel
- Insert dummy data in users table Laravel
- Laravel API response format
- The use statement with non-compound name 'Auth' has no effect
- Route [password.request] not defined
- Recursive function example code PHP Laravel
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Retain selected value of select box in Laravel
- Json encode method in laravel
- Laravel recursive function in controller
- Print last executed query in laravel
- Permission denied error while creating storage link in Laravel
- Laravel create multiple records in Pivot table
- Laravel create default admin user
- How to check find method executed successfully in laravel
- Laravel delete all rows older than 30 days