How do you change the page name for Paginator?

自作多情 提交于 2019-12-10 14:54:59

问题


Current Paginator is using ?page=N, but I want to use something else. How can I change so it's ?sida=N instead?

I've looked at Illuminate\Pagination\Environment and there is a method (setPageName()) there to change it (I assume), but how do you use it?

In the Paginator class there is a method the change the base url (setBaseUrl()) in the Environment class, but there is no method for setting a page name. Do I really need to extend the Paginator class just to be able to change the page name?


回答1:


Just like you said you can use the setPageName method:

Paginator::setPageName('sida');

You can place that in app/start/global.php.




回答2:


Just came across this same issue for 5.1 and you can pass the page name like this:

Post::paginate(1, ['*'], 'new-page-name');



回答3:


Well that didnt work for me in laravel 5 , in laravel 5 you will need to do more extra work by overriding the PaginationServiceProvider because the queryName "page" was hardcoded in there , so first create your new PaginationServiceProvider in /app/providers ,This was mine

<?php 
namespace App\Providers;

use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider as ServiceProvider;

class PaginationServiceProvider extends ServiceProvider {

    //boot 
    public function boot()
    {

        Paginator::currentPageResolver(function()
        {
            return $this->app['request']->input('p');
        });

    }//end boot 


     public function register()
    {
        //
    }


}

Then in your controllers you can do this

$users = User::where("status","=",1)
         ->paginate(5)
         ->setPageName("p");


来源:https://stackoverflow.com/questions/22731024/how-do-you-change-the-page-name-for-paginator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!