you must call paginate() instead of get(). Add whatever sorting you need to the query before calling paginate
Sep 25, 2022
9
Level 1
Collection::paginate does not exist
I'm using this package to like system,
https://github.com/renoki-co/befriended
And in this way, I show the posts that the user likes.
$liking = $this->user->liking(Post::class)->where('status',5)->get()
I want to display the last posts that have been liked - so I used the function -> reverse()
Illuminate\Database\Eloquent\Collection {#1773 ▼
#items: array:16 [▼
15 => App\Models\Post {#1796 ▶}
14 => App\Models\Post {#1797 ▶}
13 => App\Models\Post {#1798 ▶}
12 => App\Models\Post {#1799 ▶}
11 => App\Models\Post {#1800 ▶}
10 => App\Models\Post {#1801 ▶}
9 => App\Models\Post {#1802 ▶}
8 => App\Models\Post {#1803 ▶}
7 => App\Models\Post {#1804 ▶}
6 => App\Models\Post {#1805 ▶}
5 => App\Models\Post {#1806 ▶}
4 => App\Models\Post {#1807 ▶}
3 => App\Models\Post {#1808 ▶}
2 => App\Models\Post {#1809 ▶}
1 => App\Models\Post {#1810 ▶}
0 => App\Models\Post {#1811 ▶}
]
#escapeWhenCastingToString: false
}
But when I use pagination, I get an error.
dd($liking->paginate($this->perPage));
Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
Level 1
Thank you all, this code solved my problem.
public function paginate($items, $perPage = 15, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
Source:https://gist.github.com/vluzrmos/3ce756322702331fdf2bf414fea27bcb
Please or to participate in this conversation.