Solution:
There is no native support for this in Laravel.
I created a package for it: https://github.com/staudenmeir/eloquent-eager-limit
Use the HasEagerLimit
trait in both the parent and the related model.
class Category extends Model {
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}
class Product extends Model {
use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}
Then you can apply limit()
/take()
to your relationship:
Category::with(['products' => function($query) {
$query->where('name', 'LIKE', 'A%')->limit(10);
}])->get();