I have the following scope.
public function scopeWithPosts($query)
{
return $query->with(
['posts' => function($query) {
$query->limit(2); // Line 5
}]
// further eager loads
// e.g. ['posts.*']
);
}
My relationship is as follows:
public function posts()
{
return $this->hasMany('Models\Post', 'user_id', 'user_id');
}
I am attempting to do the above to reduce the load time on my query. Problem is that whenever I attempt limit, it doesn't seem to work.
For example, with Line 5 I get 1 result, and if I remove I get 5 results.
Any ideas where i'm going wrong?
Thanks in advance.
Did you already tested the take method?
$query->take(2);
For even more precise limitation you could use
$query->skip(10)->take(2);