5

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?

Marwelln
  • 28,492
  • 21
  • 93
  • 117

3 Answers3

10

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

Post::paginate(1, ['*'], 'new-page-name');
Kevin Jung
  • 2,973
  • 7
  • 32
  • 35
  • The thing is that you want this globally. You don't want to add this every time you want to use `paginate`. – Marwelln Jul 12 '15 at 11:27
5

Just like you said you can use the setPageName method:

Paginator::setPageName('sida');

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

Jason Lewis
  • 18,537
  • 4
  • 61
  • 64
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");
Big Zak
  • 1,040
  • 12
  • 17
  • I just implemented this, and it appears that with the service provider, you don't need to call `setPageName()` in your Eloquent query. – jjeaton Jan 07 '16 at 16:07