Table of contents
In this post, you will learn how to implement Laravel 9 eloquent when condition if your query has conditions this will help you not to add an if-else statement in your functionality. I will give you an example of a query with an if statement and Laravel's eloquent when condition so that it is easier for you to understand how it works.
Â
Laravel eloquent when able us to make our query cleaner and more readable without using an if-else statement in our Laravel eloquent query.
Â
Laravel Eloquent Query with If Statement
In this example, our Laravel eloquent query uses the if statement which determines from the request if the filter value is either verified or unverified users.
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request)
{
$users = User::query();
if($request->get('filter') == 'verified') {
$users->whereNotNull('email_verified_at');
}
if($request->get('filter') == 'unverified') {
$users->whereNull('email_verified_at');
}
$users = $users->get();
print_r($users); die;
}
}
Â
Â
Laravel Eloquent Query with when() method
In this example, we are using Laravel eloquent query with when()
method which is more readable and looks nicer.
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request)
{
$users = User::query();
$users->when($request->get('filter') == 'verified', function($users) {
$users->whereNotNull('email_verified_at');
});
$users->when($request->get('filter') == 'unverified', function($users) {
$users->whereNull('email_verified_at');
});
$users = $users->get();
print_r($users); die;
}
}
Â
Â
Laravel Eloquent Query with If-Else Statement
In this example, we have a query with an if-else statement which is the default is order by descending with created_at
field.
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request)
{
$users = User::query();
if($request->has('order_by') && $request->get('order_by') == 'last_login') {
$users->orderBy('last_login', 'desc');
} else {
$users->orderBy('created_at', 'desc');
}
$users = $users->get();
print_r($users); die;
}
}
Â
Â
Laravel Eloquent When() Else Example
In this example, if we have the else-if statement above then now we will have the when() else method which is still the default in ordered by descending with created_at
field. If true then it will order by last_login
.
Â
NOTE: Take note that in my example our user's table has a last_login
field.
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request)
{
$users = User::query();
$users->when($request->has('order_by') && $request->get('order_by') == 'last_login', function($users) {
$users->orderBy('last_login', 'desc');
}, function($users) {
$users->orderBy('created_at', 'desc');
});
$users = $users->get();
print_r($users); die;
}
}
Â
That's it. I hope it helps :)
Read next