php - Getting records where pivot [Laravel] -


how can records relation.

i have this:

    return conversation::with('messages')     ->get(); 

so need return convrsation in pivot table have user_id 1

it ideal if there function this:

->wherepivot('user_conversation.user_id',1) 

any solution this?

assuming conversation has belongstomany relationship setup user, can use wherehas() method constrain conversation query user relationship.

$id = 1; return converstaion::with('messages')->wherehas('users', function ($userquery) use ($id) {         $userquery->where('id', $id);     })     ->get(); 

this return conversations related user id of 1. can read more wherehas() , other useful methods under querying relationships.

another option, if have user loaded, return user's conversations:

$user = user::find(1);  return $user->conversations()->with('messages')->get(); 

Comments

Popular posts from this blog

ios - Is 'init' forbidden as *part* of a variable name? -

file - Python: AttributeError: 'str' object has no attribute 'readlines' -

c# - Get the Class name in a class with atribute inside a attribute method -