Laravel 5 pagination, won't move through pages

2019-02-27 15:45发布

问题:

I'm giving a custom page name "p_role" using setPageName method, as I want use multiple pagination on a single page.

But when I set a custom page name ($roles->setPageName('p_role');), rendered (twitter-bootstrap pagination) links are not working.

It works fine if default page name "page" is set as custom page name ($roles->setPageName('page');)

So how can I set custom page names in laravel 5 pagination.

AccessController.php

use App\Http\Controllers\Controller;
use App\Models\Role;

class AccessController extends Controller {
   public function getRole()
   {
       $roles = Role::paginate(1);
       $roles->setPageName('p_role');
       return view('role', compact('roles'));
   }
}

role.blade.php

<div>
    <ol>
        @foreach($roles as $role)
        <li>
           {{$role->name}}
        </li>
       @endforeach
    </ol>
    {!! $roles->render() !!}
</div>

Output:these links are not working, used $roles->setPageName('p_role') to set page name.

<ul class="pagination">
    <li class="disabled"><span>«</span></li> 
    <li class="active"><span>1</span></li>
    <li><a href="http://five/access/roles/?p_role=2">2</a></li> 
    <li><a href="http://five/access/roles/?p_role=2" rel="next">»</a></li>

These links works fine, used $roles->setPageName('page') to set page name.

<ul class="pagination">
    <li class="disabled"><span>«</span></li> 
    <li class="active"><span>1</span></li>
    <li><a href="http://five/access/roles/?page=2">2</a></li> 
    <li><a href="http://five/access/roles/?page=2" rel="next">»</a></li>
</ul> 
</ul> 

回答1:

It's a bug in Laravel: https://github.com/illuminate/pagination/blob/master/PaginationServiceProvider.php#L21

As you can see 'page' is hardcoded here. I submitted issue, you can now track it: https://github.com/laravel/framework/issues/8000

UPDATE:

This issue was fixed in L5.1, see pull request #8475



回答2:

try this: in controller

public function getRole()
{
   \Paginator::setPageName('foo');
    $roles = Role::paginate(1);
    return view('role', compact('roles'));
}