Laravel Eloquent When Condition Example
Hello Dev,
In this article we will cover on how to implement laravel eloquent when condition. i would like to show you laravel eloquent if condition. i explained simply step by step laravel eloquent when example. i explained simply step by step laravel eloquent when where.
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, laravel 9, laravel 10 and laravel 11 version:
let's see some examples and it will help you understand:
Simple When() Example:
Bed Way: If Condition
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::select("*");
if ($request->has('user_id')) {
$posts = $posts->where('user_id', $request->user_id);
}
$posts = $posts->get();
dd($posts);
}
}
Good Way: using when()
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::select("*")
->when($request->has('user_id'), function ($query) use ($request) {
$query->where('user_id', $request->user_id);
})
->get();
dd($posts);
}
}
When() Else Example:
Bed Way: If Condition
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::select("*");
if ($request->get('order_by') == "publish_date") {
$posts = $posts->orderBy('publish_date', 'desc');
} else {
$posts = $posts->orderBy('created_at', 'desc');
}
$posts = $posts->get();
dd($posts);
}
}
Good Way: using when()
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::select("*")
->when($request->get('order_by') == "publish_date", function ($query) {
return $query->orderBy('publish_date', 'desc');
}, function ($query) {
return $query->orderBy('created_at', 'desc');
})
->get();
dd($posts);
}
}
Multiple When() Condition Example:
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::select("*")
->when($request->has('user_id'), function ($query) use ($request) {
$query->where('user_id', $request->user_id);
})
->when($request->has('category_id'), function ($query) use ($request) {
$query->where('category_id', $request->category_id);
})
->when($request->has('title'), function ($query) use ($request) {
$query->where('title', 'LIKE', '%' . $request->title .'%');
})
->get();
dd($posts);
}
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent Model Custom Function Example
- Laravel Copy Record using Eloquent Replicate Example
- Laravel Eloquent Sum Multiple Columns Example
- Laravel Eloquent withMin(), withMax() and withAvg() Example
- Laravel Eloquent take() and skip() Query Example
- Laravel Eloquent orWhere() Condition Example
- Laravel Multiple Where Condition Example
- Laravel Relationship Eager Loading with Condition Example
- How to Use If Condition in Laravel Select Query?
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel Where Condition with Two Columns Example
- Laravel Eloquent Inner Join with Multiple Conditions Example