Pagination laravel passing data with GET - php

I have a filter for example example.com/mypage?page=2&name=Alex
And I have a lot of data with this name and the limit on pagination is 15. My problem is this. When I make the filter everything it's ok because when I press a button i'm sending with GET the values, and the first page it looks good, but when I press for the page 2 on laravel pagination the parameters are removed and it looks like example.com/mypage?page=2. What can I do?

You can just append additional parameters as described here
$users->appends(['name' => 'Alex'])->links();

Related

Pagination Via Post in Laravel

In a bit of a pickle with dealing with Pagination in Laravel. I have a hefty search query that I'm submitting via GET but the url I produce is over 255 chars (the default limit for URLs).
I would have thought the only logical way I can get around this is to use a Post method instead, but by doing this I remove all help from the Laravel Pagination tool.
The quick and easy win would be to increase my max url length on my server. But no... just no... not doing that thanks.
Can I get a suggestion on the best approach to take here?
Yes, Laravel does provide 4 parameters in paginate() method. namely paginate('per_page', ['columns_to_fetch'], 'page', 'current_page').
So, If you want to fetch 5 items per page then you need to enter per_page parameter and then you will use the current_page parameter to fetch the next or any another page.
My case was with Laravel and Vue using Element UI Pagination where i just wanted to post some params via Vue-axios and get the respective paginated data from Laravel.
Laravel pagination is able to take page value from both GET and POST request so switching to POST shouldn't break the pagination.

Bug with Php Pagination

I have a PHP page which displays n number of records in form of table from database(till here pagination works fine). Using a search field i am triggering another query, which displays the results in 2 pages. The first page shows the result fine, but when i click on the 2nd page link, it displays the result from the initial query.
any suggestions would help me to identify the actual issue that is causing this.
Thanks!
As a was stuck with same issue earlier.
The solution is to pass searching input value along pagination parameters.
In your case pagination just take it's parameter and performs its working by excluding searching query.
So just passing searching input value(Query) you will get proper result.

Pagination products with filter parameters

I haven't been working with PHP for some time and now I have to solve a problem about statement of products with pagination and filtration (by price, by the newest ones etc).
This is my url example: web.com/category-name - when I want to paginate products, I use for this purpose the $_GET variable, like: web.com/category-name?p=2.
Now to this basic pagination I would need to add parameters for filtration.
My first idea would be to send form as post and the sent data for sorting save into the SESSIONS and always check, if the parameters for filtration are in SESSION and if they are, then I will filter products.
But I am not really sure that this solution is clean... I don't want to use GET for filter parameters, because there is a lot of possible parameters...
Can I ask you, how would you solve this situation?
I prefer to use POST and save parameters information inside hidden inputs, the problem with SESSION is that if an user closes the browser/tab and goes back it will be where it was instead of the beginning.
Also, if you use the same params in another module (like page=6) of the website it could show another results at page 6 instead of the first one.

Pagination together with a html form

On my website I use a pagination (similar to that one on the bottom of this page) for MySQL output. For the change of the current page I use GET method (variable page) and it works well.
However, on my page I have also a form, using method POST, which acts as a filter for the MySQL output. This rises a problem because, when I change the form settings an submit them (POST), the page in the address line (GET) remains the same. This is problem in some cases when the filtered output has less pages than that one currently set.
Is it somehow possible to set the page variable to 0 always when the form is submitted?
Particularly, I did it using $_SERVER['REQUEST_METHOD'] == 'POST'. However, this changes just the variable in the code. Not at the address line.
On the other hand I want to keep the POST variable when I change the page of the output.
Thanks in advance.
There is a logical collision in your setup:
Unlike GET method, POST method doesn't keep variables in the address bar. But for some reason you are using POST method.
So, the solution is quite simple - use GET method for the filtering.
To create pagination links use http_build_query() out of $_GET array
In fact it's better to send filter criteria in the address (GET method). It will solve your problem with pagination and you (and your users) will have a direct link to search results.
In your PHP code, you want to redirect to the URL with the page GET parameter removed if you are SUBMITTING (by pressing the "Filter" button, or whatever it's called). So it will start at page 0.
You would have to rewrite the URL yourself (or using a plugin).
Typo corrected, I meant page 0, not 1 :P

Cakephp Pagination issue

Hi i am facing a issue with the pagination helper. I am sorting the records based on user criteria like date range etc via drop downs . And it works fine. It paginates properly. But when i click on page 2, it shows page 2 of all results an not the sorted results. How can i fix it. The fields are sorted via POST and not GET and don't want it to be GET
You can try to store the post in the session, and then to fetch it when the page is loaded.
i.e.
if(isset($this->data)){
if(isset($this->data['clear'])){ //some field (button) which will clear the session
$this->Session->delete('post');
unset($this->data);
}
$this->Session->write('post', $this->data);
}
if($this->Session->read('post')){
$this->data = $this->Session->read('post');
}
If you want take a look on this component: Filter component. The post is a little bit old, but the component is adequate still and I am using it in my projects :)
It's hard to reverse engineering your source from a screenshot but i think the main problem is you're using POST therefore when user clicks to '2' you need to use some javascript to mimick a POST request.

Categories