skip to Main Content

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


  1. 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.

    1. Security, the user will have the ability to see private data or add something in JSON and send it to the backend, and if you will not validate all data from the request in the backend, in DB will be stored wrong and dangerous data.
    2. Bad performance. As you already said data is large, so sending large data is not the optimal way.
    3. Doesn’t follow any concept.

    Will be better to send the id, status, or any fields from the form but not the collection.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search