How to automatically append query string to laravel pagination links?

后端 未结 9 1972
失恋的感觉
失恋的感觉 2020-12-01 06:06

I am working on search filter on checkbox click, with Laravel and Ajax call. So I get results when I click on a checkbox. my query is as follows:

    $editor         


        
相关标签:
9条回答
  • 2020-12-01 07:09

    Add this anywhere in your app (e.g routes.php, filters.php or anything that's autoloaded), no need to edit any pagination codes that is written already. This works flawlessly using view composers, and you don't need to know any query string parameters:

    ////////PAGINATION QUERY STRING APPEND
    View::composer(Paginator::getViewName(), function($view) {
        $queryString = array_except(Input::query(), Paginator::getPageName());
        $view->paginator->appends($queryString);
    });
    //////////////////
    
    0 讨论(0)
  • 2020-12-01 07:10

    Inspired from previous answers I ended up using the service container for both frontend + api support.

    In your AppServiceProvider@boot() method:

    $this->app->resolving(LengthAwarePaginator::class, function ($paginator) {
        return $paginator->appends(array_except(Input::query(), $paginator->getPageName()));
    });
    
    0 讨论(0)
  • 2020-12-01 07:11

    in your view where you display pagination...

    {{ $results->appends(Request::except('page'))->links() }}
    

    appends keeps the query string value except "page". not sure if it will work with POST request

    0 讨论(0)
提交回复
热议问题