2
votes

I am having a problem with Laravel Pagination and Header Sort, Let me show you what I have.

My current page I am working with is "admin/admins" where i have pagination and headers.

1) AdminController code:

// get for admin/admins page
// page to list all the Admin users
public function getAdmins($sort= NULL , $sort_dir = NULL) {

    // CACHE SORTING INPUTS
    $allowed = array('first_name', 'last_name', 'email', 'activated', 'created_at'); // add allowable columns to search on
    $sort = in_array($sort, $allowed) ? $sort : 'first_name'; // if user type in the url a column that doesn't exist app will default to first_name

    // header links setup.
    $params = Request::except(['sort','sort_dir']);
    $sort_dir = ($sort_dir == 'asc') ? 'desc' : 'asc';

    $i = 0 ;
    foreach ($allowed as $allow) {  
        $attributes[$i] = array_merge(['sort' => $allowed[$i], 'sort_dir' => $sort_dir], $params);
        $i++;
    }

    // select all admins Group = 1
    $admins = DB::table('users')
        ->join('users_roles', 'users.id', '=', 'users_roles.user_id')
        ->where('users_roles.role_id', '=' ,0)
        ->orderBy($sort, $sort_dir)
        ->paginate($this->perpage);

    // check for actions
    if (!is_null(Input::get('action'))) {

        $action = Input::get('action');

        if ($action == "add") {
             $this->layout->content = View::make('admin.admins-add');
        }
    } else {

         // get current counter admin counts
         $counter = $admins->getFrom();
         View::share('counter', $counter);

         View::share('attributes', $attributes);

         // share admin with template
         View::share('admins', $admins);

         $this->layout->content = View::make('admin.admins');
    }
}

2) route.php:

Route::controller('admin', 'AdminController');

3) view/admin/admins.blade.php (To generate the header links):

<th>{{ link_to_action('AdminController@getAdmins', 'First Name', $attributes[0]) }}</th>
<th>{{ link_to_action('AdminController@getAdmins', 'Last Name' , $attributes[1]) }}</th>
<th>{{ link_to_action('AdminController@getAdmins', 'Email' , $attributes[2]) }}</th>
<th>{{ link_to_action('AdminController@getAdmins', 'Activated' , $attributes[3]) }}</th>
<th>{{ link_to_action('AdminController@getAdmins', 'Created' , $attributes[4]) }}</th>
<th>Actions</th>

{{ $admins->links() }}

4) The {{ $admins->links() }} will generate the pagination links as we know

HERE IS MY PROBLEM the generated links looks like this :

<tr>
  <th><a href="admin/admins/first_name/asc">First Name</a></th>
  <th><a href="admin/admins/last_name/asc">Last Name</a></th>
  <th><a href="admin/admins/email/asc">Email</a></th>
  <th><a href="admin/admins/activated/asc">Activated</a></th>
  <th><a href="admin/admins/created_at/asc">Created</a></th>
  <th>Actions</th>
</tr>

Which looks fine but the problem when you go on the 2nd page , the generated links looks like:

<tr>
  <th><a href="admin/admins/first_name/asc/2">First Name</a></th>
  <th><a href="admin/admins/last_name/asc/2">Last Name</a></th>
  <th><a href="admin/admins/email/asc/2">Email</a></th>
  <th><a href="admin/admins/activated/asc/2">Activated</a></th>
  <th><a href="admin/admins/created_at/asc/2">Created</a></th>
  <th>Actions</th>
</tr>

When I click on any of them it takes me to the first page and the pagination doesn't work. How could I fix this? The pagination class doesn't have clean URL, the only way it will work if I make my URL look like this:

<tr>
  <th><a href="admin/admins/first_name/asc/?page=2">First Name</a></th>
  <th><a href="admin/admins/last_name/asc/?page=2">Last Name</a></th>
  <th><a href="admin/admins/email/asc/?page=2">Email</a></th>
  <th><a href="admin/admins/activated/asc/?page=2">Activated</a></th>
  <th><a href="admin/admins/created_at/asc/?page=2">Created</a></th>
  <th>Actions</th>
</tr>
1
You must apped array $attributes in link_to_action not just a single element. That's why it gives you wrong linksJarek Tkaczyk
Could you show me a code , when you say append what you mean?user3150060
you don't understand me , I am doing a single element to get the columns names : first_name , last_name etc..user3150060
anyone can help with this?user3150060

1 Answers

1
votes

Maybe you already get the solution by the time, but I made this:

In Your Controller:

$sort = Input::get('sort')=== '' ? 'id': Input::get('sort');
$sort_dir = Input::get('sort_dir') === 'asc' ? 'asc' : 'desc';

In your view/admin/admins.blade.php you can manually create the link like this:

First, define $page

@if($page = $admins->getCurrentPage() )@endif

Then

<th>
       @if ($sort == 'first_name' && $sort_dir == 'asc')
          <a href="{{ Request::url() }}?page={{$page}}&sort_dir=desc&sort={{$sort}}">First Name</a>
       @else
          <a href="{{ Request::url() }}?page={{$page}}&sort_dir=asc&sort={{$sort}}">First Name</a>
       @endif
</th>

Must to be another way, but this worked for me!