57

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

masud_moni
  • 1,121
  • 16
  • 33
user2067888
  • 1,085
  • 3
  • 11
  • 17

13 Answers13

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

It will append all query string parameters into pagination link

Daud khan
  • 2,413
  • 2
  • 14
  • 18
58

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

Steen Schütt
  • 1,355
  • 1
  • 17
  • 31
28

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);
Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
18

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

$paginatedCollection->appends($request->except(['page','_token']));
Sliq
  • 15,937
  • 27
  • 110
  • 143
Luca C.
  • 11,714
  • 1
  • 86
  • 77
17

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);
georaldc
  • 1,880
  • 16
  • 24
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);
});
//////////////////
Arda
  • 6,756
  • 3
  • 47
  • 67
  • FatalErrorException in routes.php line 20: Class 'Paginator' not found – Mr. Tomar Nov 26 '15 at 07:49
  • 1
    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. – Arda Nov 26 '15 at 10:09
  • View::composer(\Paginator::getViewName(), function($view) { $queryString = array_except(Input::query(), \Paginator::getPageName()); $view->paginator->appends($queryString); }); not working – Mr. Tomar Nov 26 '15 at 11:07
  • Which version of Laravel are you using ? @Tarzan – Arda Nov 26 '15 at 11:23
  • Laravel 5, Can u suggest me how i get http://localhost/search/Filipino Filipino in my controller, want a filter according to filipino – Mr. Tomar Nov 26 '15 at 12:17
  • In Laravel 5 class `Paginator` don't have `getViewName()` method. This solution is only for Laravel 4. – Marek Skiba Dec 05 '16 at 10:21
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()));
});
13

you can used request helper in view as same

{{ $users->appends(request()->query())->links() }}
xpredo
  • 1,282
  • 17
  • 27
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

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
3
  {{ $users->appends(Request::only(['filter','search']))->links()}}
2

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;
TimmyG
  • 711
  • 7
  • 5
1

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

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

Documentation here

Adam Pery
  • 1,924
  • 22
  • 21
1

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