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 ?
Why are you using latest() and sortByDesc()? How should the threads be ordered?
So what do we I do ? any suggestion or fix my code ?
You can just paginate the query:
$threads = Thread::has('replies')->latest()->paginate(12);
$threads = Thread::has('replies')->orderByDesc('replies_count')->paginate(12);
Oh mu God. You safe my life in hours. thank you @staudenmeir . I love you~
As I said, it needs to be done as part of the query, not on the result.
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 sign in or create an account to participate in this conversation.