Home > front end >  i want to get only 3 blog of per category. but code return me all blogs of per category laravel
i want to get only 3 blog of per category. but code return me all blogs of per category laravel

Time:08-10

i want to get only 3 blog of per category. but code return me all blogs of per category laravel

example i have 2 category (category 1 haves 10 blogs and category 2 haves 7 blog). now i want only 3 records of category 1 and 3 records of category 2 from DB

my controller code.

$categories = Category::has('blogs')->take(10)->get();

CodePudding user response:

Category::with('blogs')->get()->map(function($category) {
    $category->setRelation('blogs', $category->blogs->take(3));
    return $category;
});

Should do your work

  • Related