[Bug] ReorderOperation with more than 200 records
See original GitHub issueBug report
What I did
I set up a model that uses ReorderOperation
.
What I expected to happen
Reordering and saving stores the information in the database and after refresh of the view, the data is still shown with the new order.
What happened
Order was not persisted into the database. After a refresh, the old order was shown.
What is the cause of the issue
When there are more than 200 records to reorder (not an edge case with a bigger shop with multiple subcategories), the $.ajax
request data gets cut inside PHP and PHP receives only the first 200 records. The reason is that data is sent as an array from JS and PHP has a max_input_vars
which is by default 1000. Every record in the array (every model item) contains multiple properties ({item_id: "1", parent_id: 5, depth: 0, left: 11 right: 21}
), so 200 multiplied by 5.
What I’ve already tried to fix it
There are two possible solutions:
- Increase the
max_input_vars
value. I do not like this because of the possible denial of service concerns and changing the PHP ini is not always easy for all hostings. - Instead of sending an array, send the data as json string and decode it back when received in PHP. This requires two changes in the code:
-
In
vendor/backpack/crud/src/resources/views/crud/reorder.blade.php
replace$.ajax({ url: '{{ url(Request::path()) }}', type: 'POST', data: { tree: arraied }, })
with
$.ajax({ url: '{{ url(Request::path()) }}', type: 'POST', data: { tree: JSON.stringify(arraied) }, })
-
In
vendor/backpack/crud/src/app/Http/Controllers/Operations/ReorderOperation.php
replacepublic function saveReorder() { $this->crud->hasAccessOrFail('reorder'); $all_entries =\Request::input('tree'); if (count($all_entries)) { $count = $this->crud->updateTreeOrder($all_entries); } else { return false; } return 'success for '.$count.' items'; }
with
public function saveReorder() { $this->crud->hasAccessOrFail('reorder'); $all_entries = json_decode(\Request::input('tree'), true); if (count($all_entries)) { $count = $this->crud->updateTreeOrder($all_entries); } else { return false; } return 'success for '.$count.' items'; }
-
Backpack, Laravel, PHP, DB version
When I run php artisan backpack:version
the output is:
PHP VERSION:
PHP 8.0.5 (cli) (built: May 3 2021 11:30:57) ( NTS ) Copyright © The PHP Group Zend Engine v4.0.5, Copyright © Zend Technologies with Zend OPcache v8.0.5, Copyright ©, by Zend Technologies
LARAVEL VERSION:
v8.40.0@a654897ad7f97aea9d7ef292803939798c4a02a4
BACKPACK VERSION:
4.1.43@973e8ab33d42f02272f65e9688ea250fc9502d3d
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top GitHub Comments
I agree this is a breaking change, @tabacitu. I can submit a PR in the next 2 days.
Thanks! It’s work, you save my time…