0

I'm trying to pass an array through an AJAX call to the backend, the array gets pushed to the backend, but I'm getting the error "Trying to get property 'id' of non-object"

This is the

 var items = [];
  $("input:checkbox[name='refundcheck']:checked").each(function(){
    let id = $(this).val();
    let item = {
          "id": id,
          "quantity": $('#quantity'+id).val()
    };
    items.push(item);
  });

$.ajax({
          type: "GET",
          url: '{{url("/")}}/products',
          data: {items:items},
          dataType: 'JSON',
          beforeSend:function (){
                 //$("#loadingDiv").show();
          } ,
          success: function( msg ) { 
    }
});

This is the console log for that array This is the console log for that array

I've tried both of these possibilities

$productarraylist = $request->items;

foreach($productarraylist as $item){
    $product= dbtable::find($item->id);
    }

AND

foreach($productarraylist as $i=> $item){
    $product= dbtable::find($item->id);
    }

This is the var_dump result of the array in the backend.

enter image description here

I tried to JSON.decode the array on the backend, and it says I need to pass a string, not an object.

1 Answer 1

1

You are dealing with arrays and not objects, try this:

foreach($productarraylist as $item){
$product= dbtable::find($item['id']);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank YOU ! How could I possibly parse it as an object? Because the AJAX is pushing it as a JSON so why am I not able to convert it to an object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.