Advance model filtering using Laravel when method
Laravel models come with a special method call when
, The when
method provides us with the functionality to conditionally attach a query to our eloquent database query. The majority of us are used to the regular if
and else
conditional statement, but the when
method provides us with a neater and cleaner way to perform queries which is an improvement to the regular if and else statement.
At the end of this reading, you will see how you can improve your conditional queries using the when method made available for use in Laravel.
In this article, I am believing that you have basic knowledge of database querying in Laravel using the model instances as well as having your project already setup on which you want to improve on its queries.
Now let’s say we have a user
model that you want to use to perform some filters across based on certain conditions like filtering active
users, users based on their genders, cities, countries, states, even across the user names. Traditionally you may want to resolved to the below example to achieve this.
<?php
// collect our filter fields from the request parameters
$status = request()->query('status');
$gender = request()->query('gender');
$city = request()->query('city');
$country = request()->query('country');
$state = request()->query('state');
$name = request()->query('name');
// query build up
$users = User::query();
if($status){
$users = $users->where('status',$status);
if($gender){
$users = $users->where('gender',$status);
if($city){
$users = $users->where('city',$status);
if($country){
$users = $users->where('country',$status);
if($state){
$users = $users->where('state',$status);
if($name)
$users = $user->where('name','like',"%$name%");
// finally get the users
$users = $users->get();
dd($users);
Now lets us write the same query above using the Laravel eloquent method when
:
<?php
// collect our filter fields from the request
$status = request()->query('status');
$gender = request()->query('gender');
$city = request()->query('city');
$country = request()->query('country');
$state = request()->query('state');
$name = request()->query('name');
// query build up and getting users
$users = User::when($status, function($query, $status){
return $query->where('status',$status);
})->when($gender, function($query, $gender){
return $query->where('gender', $gender );
})->when($city, function($query, $city){
return $query->where('city', $city );
})->when($country, function($query, $country){
return $query->where('country', $country );
})->when($state, function($query, $state){
return $query->where('state', $state );
})->when($name, function($query, $name){
return $query->where('name','like',"%$name%");
})->get();
dd($users);
The when
method accepts two parameters, a condition that must evaluate to true for the callback to run as it first parameter followed by a callback function, which as it first parameter to be a Laravel query builder and it second parameter to be the first parameter of our when
method which will be injected by Laravel and made available within the function for it to be used. As seen above the query is not limited to any number of chained when
method, which can allow you to apply complex filters across our queries.
Conclusion
Laravel is loaded with lots of functionality to make building applications very easy and neat. The when
method is one of such, and database filtering can be improved using the Laravel when
method. Resolving your query to what we have above helps you have one URL handing all forms of filtering for a particular model.