N
N
naneri2014-12-16 08:52:46
Laravel
naneri, 2014-12-16 08:52:46

How to add pagination to a complex query in Laravel?

There is such a request in Laravel:

Topic::join('blogs', 'topics.blog_id', '=', 'blogs.id')
         ->join('blog_subscriptions as us', function ($j) use ($userId) {
            $j->on('us.blog_id', '=', 'blogs.id')
              ->where('us.user_id', '=', $userId);
          })->get(['topics.*']);

It pulls out all topics from blogs to which the User is subscribed.
How to apply pagination to it? Just in case, I'm attaching the DB schema.
1e6715e17d3949778e69b3b0cc34a07f.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
miki131, 2014-12-16
@miki131

First create relations laravel.com/docs/4.2/eloquent#relationships for the User and Blog model, and then human request

User::find(1)->blogs()->with('topics')->paginate(10)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question