Laravel 5 Eloquent where and or in Clauses

前端 未结 4 1009
轮回少年
轮回少年 2021-01-30 07:58

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          


        
4条回答
  •  醉话见心
    2021-01-30 08:32

    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.

    $feedsql = DB::table('feeds as t1')
                       ->leftjoin('groups as t2', 't1.groups_id', '=', 't2.id')
                        ->where('t2.status', 1)
                        ->whereRaw("t1.published_on <= NOW()") 
                        >whereIn('t1.groupid', $group_ids)
                       ->where(function($q)use ($userid) {
                                $q->where('t2.contact_users_id', $userid)
                                ->orWhere('t1.users_id', $userid);
                         })
                      ->orderBy('t1.published_on', 'desc')->get();
    

    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')

提交回复
热议问题