Pagination Via Post in Laravel - php

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.

Related

How to make pagination in Laravel when the URL length exceeds its limit

I am working on a project with Laravel 5.2
I have hidden ID values and I want to search the ID from the database and later I will sort it in descending order.
I wrote this code.
Controller
$jsonObject =json_decode(request('membersdata'));
$member = (new Membermasternewdata)->newQuery();
$members=$member->whereIn('id', $jsonObject)->latest();
$members=$members->paginate(50);
View
The HTML page
{{$members->appends(request()->except('page'))->links()}}
My URL example http://localhost:8000/ascendingregistrationdate?_token=ktlNHgx9PLNCOQL0CE1YZT2AzmWpJeXexqOduy8F&membersdata=%5B199%2C284%2C344%2C355%2C381%2C461%2C488%2C576%2C601%2C652%2C747%2C825%2C849%2C878%2C883%2C912%2C913%2C915%2C972%2C1004%2C1071%2C1310%2C1501%2C1534%2C1585%2C1610%2C1677%2C1854%2C1923%2C1935%2C2163%2C2301%2C2302%2C2326%2C2339%2C2717%2C2773%2C3052%2C3361%2C3446%2C3683%2C3848%2C3853%2C3914%2C4114%2C4244%2C4311%2C4357%2C4389%2C4881%2C5312%2C5322%5D&page=2
I got a problem with pagination. For around 400 records, the pagination works. But If I have many records, pagination doesn't work as the URL length exceed its limit. Is there any workaround for this problem?
Thank you!
Rather than using a GET request for the search, why don't you switch to using POST instead. That way, the memberdata array can be passed as POST data, which can have larger limits for the data (depending on your PHP config, this would be 2+ MB.
The memberdata in the URL is what's causing the URL to exceed the max limit, so you need to remove that from the URL.

Pagination laravel passing data with GET

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

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.

Symfony - Need a form filter to accept GET variable OR other method to create a filter link

Using Symfony 1.3
I have a normal form-filter that is used to filter values of a list,
it works normally. What I'd like to add is a link that is outside of the
form that can be used to filter by just a single criteria.
Anybody have a solution to this? Is there a way to set a filter to accept a GET?
You can approach this in 2 ways:
setting filters based on get parameters: symfony - admin module filters accessible as links
setting table method based on get parameters: Symfony doctrine admin generator list filters get method with no csrf token
I prefer second approach, because it gives you possibility to use filters on top your filtered list.
I resolved this in a much easier fashion than i suspected. The request already accepts GETS.
I just created a link with the parameters set as array values.
echo link_to('link_text', 'module/filter', array('query_string' => 'module_filters[field][text]='.$object->getField(ESC_RAW)))
Clicking on that link does exactly what I need.

Recommendation for a Pagination procedure AJAX PHP

I am not sure the correct terminology for the process that I am trying to describe. I don't even know which platform is underlying the technique. If you understand my description, please give the link to the site(s) and or the keyword name of the process. I think it is done by AJAX, but I am not certain. I use php as the backend code, I just need to find a way to dynamically display the results. Please give suggestions. I forgot the name of the sites that use this, and my link history expired.
TIA
Description:
The page would have a search form and options. After the user submits, the search is initiated, and the results appear inside the dedicated result area. The page does not refresh, just the info inside the result area.
The display area will show 20 (or whatever) results (lines). There will be next, and previous buttons. If you hit next, the next set of results will display.
I am writing a code that generates 20 results for each display. There is no set number of results, so the results might have a start/first page, but do not have an end page.
Each time the user hits 'next', the program would generate/load new results. It would also store previous results, so that when a user hits 'prev', the previous results can instantly come up.
What techniques/program are theses?
Having recently handled pagination with Code Igniter (php framework), the following links might help you and anyone else out:
http://tympanus.net/jPaginate/
http://codeigniter.com/forums/viewthread/93045/
Usability is important and AJAX pagination introduces some important questions that need addressing regarding pagination, the following article will give some usability guidelines for displaying results in a table:
http://thedesignvanguard.com/crud-r-for-read
Code Igniter Prototype Framework Ajax Pagination:
http://codeigniter.com/wiki/AJAX_Pagination_with_CI_Pagination_Library/
Since we're using jQuery, we chose the following, Codeigniter jQuery framework for AJAX pagination
http://tohin.wordpress.com/2008/08/12/codeigniter-ajax-pagination/
http://tohin.wordpress.com/2008/10/07/codeigniter-ajax-pagination-exampleguideline/
It is not AJAX for sure.
Nobody uses AJAX for the pagination.
And there are about zillion pagination examples over internet
The only thing is never described in articles is how to persist search options for the other pages.
But it's simple enough: http_build_query() can help you
You need to have a GET/POST form submitted via AJAX which contains the fields in the search term and page number.
If you are going to set the page size to 20 results, page_num=4 will show results from 61-80 results.
Usually search results are GET instead of POST (Ex: See Google search results ) along with the page_number as another attribute and value.
#IVAN .. good to know that the library came helpful..
About that searching option; check the recent commit in github
http://github.com/neotohin/CodeIgniter-Ajax-pagination-Library there is an additional parameter added for searching.
I think the simple code in readme is enough for understanding the mechanism.
You could use datatables
https://datatables.net/
https://datatables.net/examples/data_sources/server_side.html
With this plugin using jquery also you could use server sided page request , you just have to handle the plugin post using Ajax.
post automatically sends row , page offset column, search criteria you just have to provide the json encode return
and the plugin automatically has library in featuring pagination, search sort number of rows per page and the table formatting.
Needed are jquery, and the plugin css and js for this to work
let me know if you understood my explanation.
thanks (=

Categories