Home > Back-end >  How to pass array as a value of input while returning it from return response controller in Laravel?
How to pass array as a value of input while returning it from return response controller in Laravel?

Time:08-11

In controller

$array = [{"Id": 1, "Contract_ID": 2, "Customer_Id": 3, "Product_Id": "4", "Product_Name": "KONICA MINOLTA 226i (RADF)", "Product_Serial_key": "01"},{ "Id": 2, "Contract_ID": 2, "Customer_Id": 3, "Product_Id": "5", "Product_Name": "KYOCERA ECOSYS M2040DN", "Product_Serial_key": "02"}];

I return the above array to the blade file.

return view('blade_file_name',compact('array');

I want to store this array as the value in an input tag.

<input id="id" name="id" type="hidden" value="{{ $array }}">

now i need to take this value as array using jquery $("#id").val();

but it's throwing an error saying string object invalid.

CodePudding user response:

Use json_encode():

<input  id="id" name="id" type="hidden" value="{{ json_encode($array) }}">

Then in Javascript use JSON.parse():

const id = $("#id").val(),
      json = JSON.parse(id);
  • Related