ItSolutionStuff.com

Laravel Eloquent When Condition Example

By Hardik Savani • April 16, 2024
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, 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

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent Model Custom Function Example

Read Now →

Laravel Copy Record using Eloquent Replicate Example

Read Now →

Laravel Eloquent Sum Multiple Columns Example

Read Now →

Laravel Eloquent withMin(), withMax() and withAvg() Example

Read Now →

Laravel Eloquent take() and skip() Query Example

Read Now →

Laravel Eloquent orWhere() Condition Example

Read Now →

Laravel Multiple Where Condition Example

Read Now →

Laravel Relationship Eager Loading with Condition Example

Read Now →

How to Use If Condition in Laravel Select Query?

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →

Laravel Where Condition with Two Columns Example

Read Now →

Laravel Eloquent Inner Join with Multiple Conditions Example

Read Now →