i try to get results from table with multiple where and/or clauses.
My SQL statement is:
SELECT * FROM tbl
WHERE m__Id = 46
AND
t_Id = 2
AND
(Cab = 2 OR Cab = 4)
How i can get this with Laravel Eloquent?
My Code in Laravel is:
$BType = CabRes::where('m_Id', '=', '46')
->where('t_Id', '=', '2')
->where('Cab', '2')
->orWhere('Cab', '=', '4')
->get();
When we use multiple
and
(where) condition with last (where + or where) the where condition fails most of the time. for that we can use the nested where function with parameters passing in that.The above query validate all where condition then finally checks where t2.status=1 and (where t2.contact_users_id='$userid' or where t1.users_id='$userid')
Also, if you have a variable,
You can try to use the following code instead:
Using advanced wheres:
Or, even better, using
whereIn()
: