Laravel Eloquent When Condition Example

By Hardik Savani April 23, 2021 Category : Laravel

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 and laravel 8 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...