M
M
maddog6702019-12-17 15:22:34
Laravel
maddog670, 2019-12-17 15:22:34

How to get N news of each category in laravel using eloquent builder?

There is an array of id categories [1,2,3]. You need to choose 5 news from each category. But this is clearly the wrong solution...

return Category::whereIn('id', [1,2,3])
                ->with(
                    ['articles' => function ($query) {
                            $query->latest('published_at');
                            $query->take(5);
                        }
                    ]
                )->get();

With this code, the selection of articles takes forever on the last category

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NubasLol, 2019-12-17
@maddog670

https://github.com/staudenmeir/eloquent-eager-limit
Out of the box limit in with doesn't work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question