Multiple orWhere Condition in Laravel Eloquent
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
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 Multiple Where Condition Example
- Laravel Eloquent WhereNotIn Query Example
- How to use whereHas with orWhereHas in Laravel?
- Laravel Relationship Where Condition Example
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel WhereIn() and WhereNotIn() with Subquery Example
- Laravel Where Condition with Two Columns Example
- Laravel Where Clause with date_format() Example
- Laravel Eloquent Where Like Query Example Tutorial
- Laravel Where Clause with MySQL Function Example
- Laravel Query Builder Where Exists Example