ItSolutionStuff.com

Multiple orWhere Condition in Laravel Eloquent

By Hardik Savani • April 16, 2024
Laravel

This post will give you example of multiple orwhere condition in laravel. I’m going to show you about laravel eloquent multiple orwhere. i would like to share with you laravel multi orwhere. We will look at example of how to write multiple orwhere in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

If you need to use sql multiple or where query in laravel then you can use it. Laravel provide orWhere() to use sql or query query multiple time. in or where() we just need to pass two argument one is column name and will be value.

You can see bellow syntax on orWhere query in laravel:

orWhere(Coulumn_name, Value);

Now i will give you two example of how to use multiple orWhere query in laravel application. So let's see those example how it works.

Example 1:

SQL Query:

"select * from `users` where `first_name` LIKE ? or `last_name` LIKE ? or `email` LIKE ?"

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$search = "Har";

$users = User::select("*")

->where('first_name', 'LIKE', '%'.$search.'%')

->orWhere('last_name', 'LIKE', '%'.$search.'%')

->orWhere('email', 'LIKE', '%'.$search.'%')

->get();

dd($users);

}

}

Example 2: Multiple orWhere with where condition

SQL Query:

"select * from `users` where `status` = ? and (`first_name` LIKE ? or `last_name` LIKE ? or `email` LIKE ?)"

Laravel Query:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$search = "Har";

$users = User::select("*")

->where('status', 1)

->where(function($query) use ($search){

$query->where('first_name', 'LIKE', '%'.$search.'%')

->orWhere('last_name', 'LIKE', '%'.$search.'%')

->orWhere('email', 'LIKE', '%'.$search.'%');

})

->get();

dd($users);

}

}

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 Multiple Where Condition Example

Read Now →
ā˜…

Laravel Eloquent WhereNotIn Query Example

Read Now →
ā˜…

How to use whereHas with orWhereHas in Laravel?

Read Now →
ā˜…

Laravel Relationship Where Condition Example

Read Now →
ā˜…

Laravel Eloquent Relationships Tutorial From Scratch

Read Now →
ā˜…

Laravel WhereIn() and WhereNotIn() with Subquery Example

Read Now →
ā˜…

Laravel Where Condition with Two Columns Example

Read Now →
ā˜…

Laravel Where Clause with date_format() Example

Read Now →
ā˜…

Laravel Eloquent Where Like Query Example Tutorial

Read Now →
ā˜…

Laravel Where Clause with MySQL Function Example

Read Now →
ā˜…

Laravel Query Builder Where Exists Example

Read Now →