lundi 19 novembre 2018

How to paginate and sort results in Laravel?

I need to paginate the results and sort them using sortBy(), without losing the pagination links. I also need to use a resource to return the results.

$sorted = Model::paginate(10)->sortBy('name');

$results = \App\Http\Resources\MyResource::collection($sorted);

Doing this breaks the pagination links (I get only the data part).

$paginated = Model::paginate(10);

$results = \App\Http\Resources\MyResource::collection($paginated);

return $results->sortBy('name');

This also doesn't work. Any ideas?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire