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>