Firstly, to answer the question βWhat am I doing wrong?β, If you follow the documentation for intensive downloaded downloads, let's see that the request you created is translated as follows:
select * from messages select * from children where id in (1, 2, 3, 4, 5, ...) limit 3
(Note that I compiled the name of the children table, since I do not know which name you are using.)
After retrieving the records, Eloquent will build its object using exactly what you requested: all messages and all their children - limited to three lines . That is why you only get children for the first Message .
Now, if you want to stick to active loading, you can just get rid of the closure in the with method:
$messages = Message::select('messages.*') ->where('post_id', 1) ->where('parent_id', 0) ->with('children') ->paginate(10);
Using this paginated result, you will have access to all Messages and all your relevant children. Then, for example, in the controller, you can change your result by filtering it out or manually restricting each group of children to 3. Or you do it quickly and dirty and use the loop with break right in your view.
But do not do it quickly and dirty.
source share