Reputation: 1165
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
Upvotes: 63
Views: 115691
Reputation: 1
$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.
Upvotes: 0
Reputation: 402
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()
Upvotes: 4
Reputation: 2102
From Laravel 8 use add 'withQueryString()' to paginate:
$users = User::paginate(15)->withQueryString();
Documentation here
Upvotes: 3
Reputation: 751
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;
Upvotes: 4
Reputation: 12564
Append all input except the actual page, form token and what you don't want to pass:
$paginatedCollection->appends($request->except(['page','_token']));
Upvotes: 19
Reputation: 839
{{ $users->appends(Request::only(['filter','search']))->links()}}
Upvotes: 3
Reputation: 1504
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
Upvotes: 71
Reputation: 1523
you can used request helper in view as same
{{ $users->appends(request()->query())->links() }}
Upvotes: 13
Reputation: 155
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
Upvotes: 10
Reputation: 191
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()));
});
Upvotes: 15
Reputation: 2509
{{ $users->appends($_GET)->links() }}
It will append all query string parameters into pagination link
Upvotes: 160
Reputation: 1940
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);
Upvotes: 19
Reputation: 81157
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);
Upvotes: 30
Reputation: 6916
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);
});
//////////////////
Upvotes: 16