mardi 13 octobre 2015

Laravel 5.1: Eloquent relationship hasmany, Limit records

I have a problem with Laravel 5.1: Eloquent relationship hasmany, Limit records I have 2 tables: feeds, comments. The request is to obtain 5 feeds and comments accordingly to each particular feed. I am currently using the below query:

public function getFeed($user_id)
{
    return Feed::whereUserId($user_id)->with(['comments'])->take(10)->get()->map(function ($feed) {
        $feed->comments = $feed->comments->take(5);
        return $feed;
    });
}

However, it return all the comments. My thinking was that the $feed->comments = $feed->comments->take(5); line doesn't work. I only want to get 5 comments for each feed, do you have any advise? Any comments are highly appreciated. Thank you!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire