mercredi 24 janvier 2018

Get exact count of posts from grouped Eloquent collection

I have two tables, 'videos' and 'categories'. In my VideoController file I created a private function

private function getHomePageVideos($lang = 'en')
{
    return \DB::table('videos')
    ->leftJoin('categories', 'categories.id','=','videos.category_id')
    ->select('videos.*', 'categories.title_' . $lang . ' as category', 'categories.slug as category_slug')
    ->get()->groupBy('category');
}

This returning me a collection, in my case, with two groups:

    Collection {#910 ▼
  #items: array:2 [▼
    "Master Classes" => Collection {#907 ▶}
    "Day By Day" => Collection {#908 ▶}
  ]
}

I need to get only 3 posts from each group.

I tried 'take(3)', but it consumer to categories (I have 2 in my project), not posts.

Thanks In Advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire