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.
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.