
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
- How to get last record from object collection in laravel
- Run artisan command to generate key in laravel
- How to use bootstrap pagination in laravel 8
- Get last record from table in laravel
- How to add a key value pair to existing array in laravel
- Drop foreign key column in Laravel using migration
- How to restore deleted records in laravel
- Shorter syntax for whereHas with call back function in laravel
- Laravel order by date not working
- How to call controller function from view in Laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- How to insert dynamic value to additional column in pivot table in laravel
- Laravel csrf token mismatch for ajax POST Request
- Get products with number of orders in Laravel
- How to pass link from controller to view in laravel on ajax call
- How to get single column value in laravel
- How to print form data in laravel
- Call to undefined relationship [user] on model [App\Models\Post]
- Automatically remove records using Prunable trait in Laravel
- Update email with unique validation in laravel
- How to remove P tag from CkEditor in Laravel?
- Trying to get property 'title' of non-object
- Laravel onclick function not working
- How to check data inserted or deleted in pivot after toggle method
- Update existing pivot table data in laravel