Reputation: 1368
I have used the following code for conditional where query. But I didnt get the result in foreach statement but if I use $member->first() I am getting the result. Is this the correct way to use conditional where in laravel?
$member = Member:query();
if(!empty($param['activity'])){
$member->where('activity', 'LIKE', $param['activity'] .'%');
}
if(!empty($param['MemberName'])){
$member->where('MemberName', 'LIKE', $param['MemberName'] .'%');
}
if(!empty($param['Designation'])){
$member->where('Designation', 'LIKE', $param['Designation'] .'%');
}
$member->take(100)->skip($offset)->get();
foreach ($member as $arr)
{
....
}
Upvotes: 0
Views: 145
Reputation: 152860
This is totally the right way to do it. However you're missing one little thing. get()
will return the result but the $member
will still be the query builder instance. What you need to do is assign the result to $member
$member = $member->take(100)->skip($offset)->get();
Upvotes: 1