Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5.1 : how can i have two wherePivot in eloquent?

I need check two where conditions on a pivot table. I know that I can check one condition with this:

$dis = $user->discounts()->wherePivot('used_for_id', '=', null)

However, I want two where conditions. When I use orWherePivot, the two where conditions are ORed together, but I want them to be ANDed together.

$whereData = [
    ['id', "=", $discountId],
    ['used_for_id', "=", null]
];
like image 579
Hamid Naghipour Avatar asked Jan 12 '17 08:01

Hamid Naghipour


1 Answers

wherePivot() works the same as a normal where() method; you can just chain on the second wherePivot() condition and it will be ANDed with the previous conditions:

$dis = $user
    ->discounts()
    ->wherePivot('id', '=', $discountId)
    ->wherePivot('used_for_id', '=', null)
    ->get();
like image 176
patricus Avatar answered Nov 14 '22 21:11

patricus