Eloquent orwhere Query Use in Laravel

Today’s Article is how to use or where condition in laravel eloquent. How works laravel orWhere() query. We will give you easy example of or where condition in laravel query builder. you can use laravel eloquent or where simply.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

How to Use orWhere in Laravel?

If you required to use sql or where query in laravel then you can use it. Laravel provide orWhere() clauses to use phpmyadmin run sql or query query. in or where() we just required to pass two argument one is Database column name as well as will be value.

You can see following the syntax on orWhere query in laravel:

orWhere(YourDBTableCoulumn_name, Value);

Laravel orWhere Condition using Eloquent Query

Now We will give you 4+ Best example of how to use orWhere query in laravel any projects(application). So let’s see those example how it works OR in a Where Clause with Laravel’s Query Builder.

using SQL Query:

SELECT *
  FROM employees
  WHERE id = '41' OR email = '[email protected]'

Laravel Query:

public function index()
{
    $employees = DB::table('employees')
                    ->where('id', 41)
                    ->orWhere('email', '[email protected]')
                    ->get();
  
    dd($employees);                    
}

using SQL Query:

Here is another example of how it works with model. you can work as like bellow:

public function index()
{
	$employees = Employee::select("*")
	        ->where('id', 41)
	        ->orWhere('email', '[email protected]')
	        ->get();
    dd($employees);                    
}

Or Statements in Laravel

You may row where limitations both as well as add or clauses to the database query. The orWhere Clauses manage the each parameters as the where Clauses:

$employees = DB::table('employees')
		->where('votes', '>', 98)
		->orWhere('empname', 'RahulGandhi')
		->get();

If you required to same group an “or” where condition within putting “brackets”, you may pass a Closure as the first parameter to the orWhere Clauses:

Laravel Query:

$employees = DB::table('employees')
    ->where('votes', '>', 98)
    ->orWhere(function($query) {
        $query->where('empname', 'Yagnika')
              ->where('votes', '>', 45);
    })
    ->get();

using SQL Query:

// SQL: select * from employees where votes > 98 or (empname = 'Yagnika' and votes > 45)

laravel multiple orWhere condition(nested orwhere in laravel)

here learn about Laravel eloquent with multiple where and orWhere clauses.
nested orWhere

$empname = $_POST['empname']; // I'd recommend you use Input::get('empname') instead...

$employee_list = DB::table('employees')
    ->where('employees.employee_id' ,'=', $clientId)
    ->where(function($query) use ($empname){
        $query->where('employees.firstname', 'LIKE', '%'.$empname.'%');
        $query->orWhere('employees.lastname', 'LIKE', '%'.$empname.'%');
        $query->orWhere('employees.email', 'LIKE', '%'.$empname.'%');
    })
    ->join('employees_roles', 'employees.id', '=', 'employees_roles.employee_id')
    ->where('employees_roles.role_id', '=', Role::USER_PARTICIPANT)
    ->get();

AND-OR-AND + brackets with Eloquent

Right way – putting “brackets” into Laravel Eloquent Advanced Where Clauses – where we can assign a function to a where clause, like this:

// ...
$q->where(function ($query) {
    $query->where('gender', 'Male')
        ->where('age', '>=', 15);
})->orWhere(function($query) {
    $query->where('gender', 'Female')
        ->where('age', '>=', 45);	
})

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about orwhere in laravel.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP Laravel Redirect with Query String Example
  2.   WPML Display Different Logo in each language
  3.   Laravel 5.8 pdf create Download and Print
  4.   Laravel 6 Group By using Eloquent
  5.   How to display break in Laravel blade properly?
  6.   Registration Form Validation using Ajax in Codeigniter
  7.   IMPLODE() AND EXPLODE() FUNCTION IN PHP
  8.   PHP MySQL LIKE operator string Function
  9.   Laravel Grouping Cache Offset & Limit Eloquent methods
  10.   Laravel Create Read Update Delete Query Builder