Support the ongoing development of Laravel.io →
posted 7 years ago
Eloquent
Last updated 2 years ago.
0

Fixed an error in the question regarding the subquery. Should make more sense now.

0
->with(['children' => function ($query)
{
   $query->take(3);
}])

will take 3, but for whole collection.

I would make a new relationship something like children3

return $this->hasMany('App\Message', 'parent_id', 'id')->take(3);

and then

$messages = Message::select('messages.*')
        ->where('post_id', 1)
        ->where('parent_id', 0)
        ->with('children3')
        ->paginate(10);
Last updated 7 years ago.
0

I tried your recommendation (children3), but it's still only returning a total of 3 children (i.e. only for the first parent result).

0

Sign in to participate in this thread!

Eventy

Your banner here too?

user732156 user732156 Joined 11 Jun 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.