Laravel Eloquent when Condition Code Example
Teams applying “Laravel Eloquent when Condition Code Example” in a production project can use Monitask to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hello Friends,
You will discover how to use the Eloquent when condition in Laravel 9 in this article. This will assist you avoid adding an if-else statement to your functionality if your query contains conditions. To make it simpler for you to grasp how it functions, I'll give you an example of a query that incorporates both Laravel's elegant when condition and an if statement.
Laravel eloquent added new function call when(), using when() you can ignore to write manually if conditional statement. i will give you some example how to use it when and how to write if condition with eloquent query builder. you can also use when() with laravel 6, laravel 7, laravel 8 and laravel 9 version:
Step 1: Install Laravel
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-appStep 2: Laravel Eloquent Query with If Statement
In this case, our Laravel eloquent query uses an if statement to evaluate whether the filter value is made up of authenticated users or unverified users based on the request.
App/Http/Controllers/UserController
<?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;
}
}Step 3: Laravel Eloquent Query with when() method
App/Http/Controllers/UsersController
<?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;
}
} Step 4: 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.
App/Jobs/Controllers/UserController
<?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;
}
}Step 5: 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.
App/Jobs/Controllers/UserController
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;
}
}I hope it can help you...
# Laravel
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial