lsutiger's avatar

Child Relationship Pagination

I have my models set up and am able to return parent/child relationships between two tables; quotes and authors. I am wanting to query the authors table and return the paginated results of any related quotes associated with each author for use in a view. In my current situation, I am returning one author and their related quotes but I cannot seem to get a handle on the pagination. When I run the following code, I get no error and the results are as they seem they should be but without pagination. How do I paginate these child rows?

$author = Author::with(['quotes' => function ($q)
            {
                $q->where('active', '=', 1)
                    ->paginate(15);
            }])
            ->where('active', '=', 1)
            ->whereRaw('authors.slug = ?', array($id))
            ->limit(1)
            ->get();
0 likes
7 replies
lsutiger's avatar

@jlrdw Thanks for the reply. I have my relationships set up in the model and can query the quotes table separately to establish pagination. In fact this is how I had it before I began trying to see if it could be accomplished more efficiently. Maybe that is the best it gets but I am curious if there is a way to have a Paginator on child rows so that I can render the links in the view. The result I get with the above code is the parent/child relationship where I loop through the quotes in my view. The number specified per page [ paginate(15) ] is the number of child rows returned but there is no paginator attached.

jlrdw's avatar

Should your bottom get be changed to paginate

lsutiger's avatar

That gives me a paginator but it is on the author and not the quote detail.

jlrdw's avatar

When I am doing this sort of thing I normally just customize my own code and my own paginator to suit my needs at the time. Normally a complex report is done as a single page app not so much in MVC so that's the way I treat a complex query, meaning a complex report style. Look at the first answer examples.
In fact for some things I don't even use Laravel or frameworks I either have a stored procedure to do the job or custom written PDO single page type program to handle it.
But normally this is only for a specialized type run, not run of the mill CRUD.
Look at the paginator in this video https://drive.google.com/file/d/0B1_PFw--3o74RDhUNjIxSVNiQ2M/view?usp=sharing All I needed here was a super quick next and previous type thing so I just slapped my own together.

jlrdw's avatar
jlrdw
Best Answer
Level 75

I just thought of the way I would solve your problem, that is if you are trying to get all authers with articles.
Put the author at the top of the page with one query, right below this paginate the articles, have a custom header which has the author in it and maintain that Arthur until you go to the next Arthur, something like that where the auther is repeated at the top as necessary, in a case like this a separate query may be easier to write. If you look at the videos that Jeffrey has done at runtime a one-to-many is converted into two queries anyway.

lsutiger's avatar

Yeah, that last comment you made is basically how I had implemented it and was thinking I must be missing something where it could be achieved with one chained statement. The paginator works really good that way in laravel. Thanks again for taking the time to answer.

Please or to participate in this conversation.