title: How to paginate and sort results in Laravel?
body: <p>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.</p>
  
  <pre><code>$sorted = Model::paginate(10)-&gt;sortBy('name');
  
  $results = \App\Http\Resources\MyResource::collection($sorted);
  </code></pre>
  
  <p>Doing this breaks the pagination links (I get only the data part).</p>
  
  <pre><code>$paginated = Model::paginate(10);
  
  $results = \App\Http\Resources\MyResource::collection($paginated);
  
  return $results-&gt;sortBy('name');
  </code></pre>
  
  <p>This also doesn't work.
  Any ideas?</p>