Instead of making multiple joins everywhere in your project, define a relationship.
This is a nice example of a Many-to-Man relationship.
class Item...
public function likers(){
return $this->belongsToMany('User', 'likes_table');
}
Use the relationship to retrieve users for the item.
$usersWhoLikeThisItem = $item->likers()->get();
or the shortcut form
$usersWhoLikeThisItem = $item->likers;
You can also use eager loading to make your project faster.
$newItems = Item::with('likers')->where('created_at', '>', $time)->get();
foreach($newItems as $newItem)
{
$likers = $newItem->likers;//already loaded
foreach($likers as $user){
echo 'PictureLink:'.$user->profile_picture_url.'<br/>';
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community