65

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:

$editors = User::with(['editor.credentials','editor.specialties','editor.ratings']);
$temp=$editors->whereHas('editor', function($q) use ($a_data){
    $q->whereHas('specialties',function($sq) use($a_data){
        $sq->whereIn('specialty',$a_data);
    });
})->paginate(2);

This gives me all the data I need. however how should I get the links for pagination?

$temp->setBaseUrl('editors');
$links = $temp->links()->render();

I am currently doing this and with $links which I am sending over as response to ajax call, I set the pagination with $links data. Now, I need to append the query to next page like page=2?query="something". I don't know how should I go about appending the remaining query result links to next page links. i.e. I don;t know what should come in the query="something" part. Can someone guide me. thanks

15 Answers 15

160
{{ $users->appends($_GET)->links() }}

It will append all query string parameters into pagination link

Sign up to request clarification or add additional context in comments.

3 Comments

Based on this, I used request helper in view: {{ $users->appends(request()->query())->links() }}
Or even better {{ $posts->appends(request()->except('page'))->links() }}
@FosAvance thanks. this hadn't any value added. worked like Ehsan code for me. what is the difference??
71

As of Laravel 7, you can call the withQueryString() method on your Paginator instance.

Quote from the documentation:

If you wish to append all current query string values to the pagination links you may use the withQueryString method:

{{ $users->withQueryString()->links() }}

See "Appending To Pagination Links": https://laravel.com/docs/7.x/pagination#displaying-pagination-results

3 Comments

You can also apply this directly to your model query. User::paginate()->withQueryString() which lets you do this inside your view: {{ $users->links() }}
Laravel 10 docs no longer show this method of doing it, however it does work with the current version 10.
Laravel 11 has the withQueryString() documentation @Chris-MindflowAU laravel.com/docs/11.x/…
30

Check the answer from @Arda, as it's global solution. Below you can find how to do it manually.

Use appends on Paginator:

$querystringArray = Input::only(['search','filter','order']); // sensible examples

// or:
$querystringArray = ['queryVar' => 'something', 'anotherVar' => 'something_else'];

$temp->appends($querystringArray);

Comments

19

For the latest version of Laravel at the moment (5.2), you can just use the Request facade to retrieve the query string and pass that to your paginator's appends() method

$input = Request::input();
$myModelsPaginator = App\Models\MyModel::paginate();
$myModelsPaginator->appends($input);

Comments

19

Append all input except the actual page, form token and what you don't want to pass:

$paginatedCollection->appends($request->except(['page','_token']));

Comments

16

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);
});
//////////////////

6 Comments

FatalErrorException in routes.php line 20: Class 'Paginator' not found
This Answer is for Laravel 4 @Tarzan, In Laravel 5, classes are PSR-4 standards, so you need to add \ character before. But I've not tried this in LAravel 5.
View::composer(\Paginator::getViewName(), function($view) { $queryString = array_except(Input::query(), \Paginator::getPageName()); $view->paginator->appends($queryString); }); not working
Which version of Laravel are you using ? @Tarzan
Laravel 5, Can u suggest me how i get localhost/search/Filipino Filipino in my controller, want a filter according to filipino
|
15

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()));
});

2 Comments

worth notting, that you better bind both paginator types, lenghaware and normal
this helped me solve my problem. I added a new answer for Laravel 8
13

you can used request helper in view as same

{{ $users->appends(request()->query())->links() }}

Comments

10

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

Comments

6

Laravel 10 can use withQueryString

Example: $users = User::paginate(15)->withQueryString();

https://laravel.com/docs/10.x/pagination#appending-query-string-values

Or, when displaying the links using the helper method, however it appears that method is not documented any more (not that I could find using the search bar in their docs).

$users->withQueryString()->links()

Comments

4

Updating @rasmus answer for Laravel 8.

In your AppServiceProvider boot method, add the following lines and your existing query string will be be used for all pagination links

$this->app->resolving(Paginator::class, function ($paginator) {
    return $paginator->appends(Arr::except(Request::query(), $paginator->getPageName()));
});
$this->app->resolving(LengthAwarePaginator::class, function ($paginator) {
    return $paginator->appends(Arr::except(Request::query(), $paginator->getPageName()));
});

And for completeness, use these classes:

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Request;

1 Comment

2022 This should be the accepted answer!
3
  {{ $users->appends(Request::only(['filter','search']))->links()}}

Comments

3

From Laravel 8 use add 'withQueryString()' to paginate:

$users = User::paginate(15)->withQueryString();

Documentation here

Comments

3

Simply use ->paginate(5)->withQueryString()

Comments

0

$this->app->resolving(LengthAwarePaginator::class, function ($paginator) { return $paginator->appends(Arr::except(Request::query(), $paginator->getPageName())); });

Add the following line to the AppServiceProvider.php file to include the query string in all paginations across the application. Make sure to import classes.

1 Comment

You don't need to copy and paste already given answers as yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.