Here is how I am trying to paginate:
$posts = Post::all()->sortByDesc("created_at")->pagination(1);
But I get this error:
Method Illuminate\Database\Eloquent\Collection::pagination does not exist.
It is because paginate
is a Builder
method, not a collection.
You need to create paginator manually. How is described on Manually Creating A Paginator.