I want to send laravel collection from html button
button:
<button url="{{ route('get.pk',$pm) }}" data-pm="{{ json_encode($pm) }}"> <i class="fa fa-arrow-down text-white"></i> </button>
ajax request:
let pm = $(this).data('pm');
if(check.length < 2){
$.ajax({
url: ini.attr('url'),
method: "POST",
dataType: 'json',
data: {_token: "{{ csrf_token() }}",
pm: pm},
}).done(function(data){
$(loc).replaceWith(data);
}).fail(function(data){
$(loc).replaceWith('<tr><td colspan="16"><div align="center">Failed!</div></td></tr>');
});
}
in laravel controller:
$pmData = $request->input('pm');
I want to use the variable from that ajax, but is it safe? cause i was sending large collection.
and the reason i want to use this was because i eager loaded from $pm and the data is already filtered there, so instead of sending id and filter it again, i want to just filter it once and send it to controller to use it.
it is so that when i click arrow it return the data from that row’s child
first, i got error max_input_vars but i already fix it with increasing in php.ini, but i was wondering is it safe and make my website performances bad?
or is there any other way to filter data beside this method, if so please tell me thanks
2
Answers
This way is not good. The concept to save all necessary data on the client side and sending it to the backend is bad. Below is a list with some (but not all) of why it is a bad solution.
Will be better to send the id, status, or any fields from the form but not the collection.
It is possible but not a good choice to make, I’ll try to explain potential issues and porper way to address them of you still want to continue, Sending a large Laravel collection as a variable through Ajax to controllers is possible, but there are a few considerations to keep in mind regarding safety and performance.
Safety
Network Latency
Sending a large amount of data over Ajax can introduce additional latency due to the time it takes to transmit the data over the network. This might not be a significant issue for moderate-sized collections, but for very large collections, it could impact user experience.
Server Processing
On the server side, processing a large collection could consume more memory and CPU resources. This could potentially affect the responsiveness of your application if multiple large requests are being processed concurrently.
Alternatives
Pagination
Instead of sending the entire collection at once, consider implementing pagination on the server side. This way, you only send a subset of the data to the client, and they can request more pages as needed.
Filtering on Server
Since you mentioned that the data is already filtered in the client-side collection, consider if it’s possible to send the filter parameters to the server and apply the filtering there. This can help reduce the amount of data transferred while ensuring that the filtering logic is consistent.
Caching
Depending on the nature of your data, you might be able to implement caching strategies to reduce the need for repeated filtering and processing of the same data.
In conclusion, it is possible to do what you are intending to do here, but this should be done with proper safety measures in plan.