
How to use more than one query scope in Laravel
How to use more than one query scope in Laravel
In this article, we will see how to solve "How to use more than one query scope in Laravel".
You can use (call) more than one query scope in Laravel after defining the query scopes in the model. You can call the multiple query scopes using the chaining method.-
Chaining query scopes in Laravel
--PATH app\Models\User.php<?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Event; class User extends Authenticatable { public function scopeActive($query) { return $query->where('active', 1); } public function scopeRegisteredWithinDays($query, $days) { return $query->where('created_at', '>=', now()->subDays($days)); } } //Calling query scopes method $users = App\Models\User::registeredWithinDays(30)->active()->get();
Chaining query scopes will help you to use the multiple scopes while querying the model. You can get the user records from the users table which are created within 30 days and are active (active column has value 1).
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
- PhpMyAdmin - Error The mysqli extension is missing
- How to use bootstrap pagination in laravel 8
- Undefined property: stdClass::$title
- Get comma separated email from input array
- Convert input array to comma-separated string in laravel controller
- How to set column as primary key in Laravel model
- How to get CSRF token in laravel controller
- Get current month records in laravel 7/8
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- Rename Pivot Table in Laravel
- How to get the random value form a specific column in laravel ?
- Laravel URL validation not working
- The POST method is not supported for this route. Supported methods: PUT.
- How to get specific columns using Laravel eloquent methods
- How to validate website url in laravel using validaiton
- How to pass query string to url in laravel
- Laravel insert query not working
- Global scope in Laravel with example
- Credit card validation in laravel
- Display first n record from collection in laravel view
- Property [user] does not exist on this collection instance
- Call to undefined method App\Models\User::follow()
- 419 page expired error in Laravel
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- Get last year created records in Laravel