laravelmodel-view-controllerpaginationlaravel-query-builderlaravel-collection

How can I paginate a Laravel collection?


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.


Solution

  • 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.