Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically append query string to laravel pagination links?

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

like image 813
user2067888 Avatar asked Jul 22 '14 15:07

user2067888


People also ask

What is withQueryString in Laravel?

Laravel provides us predefined function call withQueryString and you may use this method to append all of the current request's query string values to the pagination links.

How does Laravel pagination work?

Pagination works by selecting a specific chunk of results from the database to display to the user. The total number of results is calculated and then split between pages depending on how many results you want to return on a page.


2 Answers

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

It will append all query string parameters into pagination link

like image 180
Daud khan Avatar answered Sep 18 '22 09:09

Daud khan


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

like image 21
Steen Schütt Avatar answered Sep 22 '22 09:09

Steen Schütt