C
C
chelkaz2017-03-25 06:18:01
Laravel
chelkaz, 2017-03-25 06:18:01

How to make Paginate for a related model (relationship)?

I have a selection, fully optimized queries, and exactly in such a collection as needed, but what about pagination?
Is it possible to add pagination for relations:
with('posts', 'posts.comments', 'posts.user', 'posts.comments.user') ?
Or is it not acceptable in this sample?

$locations = Locality::where('loc_id', $id)
            ->with('posts', 'posts.comments', 'posts.user', 'posts.comments.user')
            ->firstOrFail();

----------------------------------------
But if so, then:
$locations = Locality::where('loc_id', $id)
            ->with(['posts' => function($query) {
                $query->paginate(5);
            }], 'posts.comments', 'posts.user', 'posts.comments.user')
            ->firstOrFail();

This is how pagination works, more precisely, it displays 5, but there is no pagination template...
If I try to display pagination:
{{ $locations->posts->links() }}
That's an error...
Method links does not exist.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Frank, 2017-03-25
@Akellacom

https://laracasts.com/discuss/channels/laravel/how...
https://laracasts.com/discuss/channels/eloquent/ho...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question