davidlm75's avatar

Pagination and column sorting

I'm trying to use pagination and sort the columns on each page. It doesn't sort just the 10 records retrieved, but the entire table. When I change the page it loses the sort order and goes back to the default.

public function index() { if(Auth::check()) { $sortby = Input::get('sortby'); $order = Input::get('order');

        if($sortby && $order) 
        {
            $child = Child::orderBy($sortby, $order)->paginate(10);
        }else{
            $child = Child::paginate(10);
        }

        return View::make('childinfo.index', compact('child','sortby', 'order'));
    }else{
        return Redirect::to('login');
    }
}
0 likes
12 replies
bestmomo's avatar

If you do an orderBy in query all records are sorted. If you want to sort only the result of pagination you must use sortBy on the collection :

$child = Child::paginate(10)->sortBy($order);
1 like
davidlm75's avatar

@bestmono When I use your solution I get the following error: Call to undefined method Illuminate\Support\Collection::links()

bestmomo's avatar

Edit because bad test.

The sort breaks the links so you must get them before :

$child = Child::paginate(10);
$links = $child->links();
$child = $child->sortBy($order);

But you must send $links to the view and edit for the links :

{{ $links }}
nfauchelle's avatar

@davidlm75 Don't you want it to sort the whole table? If I am on the first page, and sort by name, it should have A on that first page and Z should be on the very last page of results... that is typically how sorting would work....

davidlm75's avatar

@nfauchelle That's what I'm attempting to do, however when I change pages the sort reverts back to the default.

davidlm75's avatar
 @if($sortby == 'childfirstname' && $order == 'asc'){{
                link_to_action(
                'ChildController@index',
                'First Name',
                  array(
                    'sortby' => 'childfirstname',
                    'order' => 'desc'
                  )
                )
            }}
            
            @else {{
              link_to_action(
              'ChildController@index',
              'First Name',
                array(
                  'sortby' => 'childfirstname',
                  'order' => 'asc'
                )
              )
            }}
            
            @endif 

   $order = Input::get('order');
   $sortby = Input::get('sortby');
   
   
   if($sortby && $order) 
   {
     $child = Child::orderBy($sortby, $order)->paginate(10);
   }else{
     $child = Child::paginate(10);
   }

   return View::make('childinfo.index', compact('child','sortby', 'order'));
davidlm75's avatar

I appreciate the help but none of this has worked for me. I've read the documentation, scoured the internet for a solution with no luck. The documentation seems to be vague in many areas and I learn from trial and error. I will continue to look for a solution.

bestmomo's avatar
Level 52

Change your code :

if($sortby && $order) 
{
   $child = Child::orderBy($sortby, $order)->paginate(10);
   $links = $child->appends(['sortby' => $sortby, 'order' => $order])->links();
} else {
   $child = Child::paginate(10);
   $links = '';
}
return View::make('childinfo.index', compact('child','sortby', 'order', 'links'));

In your view you have your links like that :

{{ $child->links() }}

Change for :

{{ $links }}

Must work like that...

1 like
davidlm75's avatar

@bestmomo the only snippet of code I needed to change was


$links = '';

to:


$links = $child->links();

The pagination wasn't displaying using $links = '';

Anyhow the code now works the way expected.

1 like

Please or to participate in this conversation.