irsyadadl's avatar

Add Pagination to This Collection

How can I make it to be paginated ?.

Here is my code.

$threads = Thread::has('replies')->latest()->get()->sortByDesc(function ($thread) {
    return $thread->replies_count;
});
                

I try to make like this.

$something = $threads->paginate(12);
 dd($something);

But the error came like this.

Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

Anyone of you guys understand how to make it to be paginated ?

0 likes
7 replies
staudenmeir's avatar

Why are you using latest() and sortByDesc()? How should the threads be ordered?

irsyadadl's avatar

So what do we I do ? any suggestion or fix my code ?

1 like
staudenmeir's avatar
Level 24

You can just paginate the query:

$threads = Thread::has('replies')->latest()->paginate(12);

$threads = Thread::has('replies')->orderByDesc('replies_count')->paginate(12);
1 like
Cronix's avatar

As I said, it needs to be done as part of the query, not on the result.

jlrdw's avatar

I wish newbies would forget about collections and use query results, they are just array's used to manipulate data.

A collection is not meant to hold thousands of record results.

Please or to participate in this conversation.