#Adding a limit to eager loading a relationship

2 messages · Page 1 of 1 (latest)

tough geode
#

I have the following code:

Province::query()
    ->with(['cities' => function ($query) {
        $query->take(10);
    }])
    ->get()

However, this just adds a LIMIT 10 to the end of the relationships query. Resulting in a total of 10 cities over all the provinces. How would I make it so I get 10 cities per province?

I've stumbled upon this package, but I'd rather not use a package for something this 'simple': https://github.com/staudenmeir/eloquent-eager-limit

forest trout
#

I think that package would be the only way to achieve it (or, do what the package does; build it yourself)