phplaravelpaginationlaravel-5laravel-blade

Use Pagination With sortBy in Laravel?


I'm trying to pull a few rows with a model in Laravel using pagination and sortBy.

Here is my Controller (without sorting):

    public function all(){
       $results['rows'] = Items::where('XXXX','YYYY')->paginate(32);
       return view('page', $results);
    }

This works fine. When I add sorting to it:

public function all(){
       $results['rows'] = Items::where('XXXX','YYYY')->paginate(32)->sortBy('TTT');
       return view('page', $results);
    }

The pagination on the blade template crashes. This is my code in blade:

<?php echo $rows->render(); ?>

I read a few questions here with a similar question, though the code didn't seem to be like mine and couldn't figure out how to implement it on my code. Any help would be much appreciated.


Solution

  • As Viney said, use orderBy() instead of sortBy():

    $results['rows'] = Items::where('XXXX','YYYY')->orderBy('TTT')->paginate(32);