I have a many-to-many relation set on my User and Notification Eloquent models. This way I can access the pivot table - user_notifications - as follows:
$user = User::find(1);
foreach ($user->notifications() as $n) {
echo $n->pivot->created_at;
}
This will give me all created_at
field values from the pivot table, for the user of ID = 1.
What if I need only one pivot row, let's say the one with notification_id = 2? Is there a way to combine pivot
with where
or has
? Can it be done without looping through $user->notifications()
?