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]
];
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With