how write laravel eloquent below sql query ? username,first_name,last_name , email column names.
select * `message_users` `username` %somthing% , (`first_name` %somthing% or `last_name` %somthing%) , `email` %something%
i've tried
$queryobj= $this; if(!empty($filter['username'])) $queryobj = $queryobj->where('username','like','%'.$filter['username'].'%'); if(!empty($filter['name'])){ $queryobj = $queryobj->where('first_name','like','%'.$filter['name'].'%'); $queryobj = $queryobj->orwhere('last_nam','like','%'.$filter['name'].'%'); } if(!empty($filter['email'])) $queryobj = $queryobj->where('email','like','%'.$filter['email'].'%'); $result = $queryobj->orderby('id','desc')->paginate($limit); return $result;
but builds query without round brackets. don't give me correct result!
select * `message_users` `username` %somthing% , `first_name` %somthing% or `last_name` %somthing% , `email` %something%
you can use advanced clauses - parameter grouping . query inside closure should contained within parenthesis group.
if(!empty($filter['name'])){ $queryobj->where(function ($query) use ($filter) { $query->where('first_name','like','%'.$filter['name'].'%') ->orwhere('last_name','like','%'.$filter['name'].'%'); }); }
Comments
Post a Comment