Skip to content
Advertisement

How to convert object sent from angular into associative array in laravel?

This is my array request that is received by back-end laravel. I want to insert a bulk of rows. But I should convert it into an associative array. How can I convert my received array data into the format I want.

Array of Objects that I want to convert:

[
  {
   "reservation_id": 25,
   "room_id": 1,
   "check_in_date": "2020-04-27 12:00:00",
   "check_out_date": "2020-04-30 12:00:00",
   "availability": false,
   "status": "booked",
   "booking_id": 26
 },
 {
  "reservation_id": 26,
  "room_id": 2,
  "check_in_date": "2020-04-27 12:00:00",
  "check_out_date": "2020-04-30 12:00:00",
  "availability": false,
  "status": "booked",
  "booking_id": 26
 }
] 

dd($request->all()) gives me below value.

enter image description here

Advertisement

Answer

You can use json_decode for that:

MyModel::insert($request->all());
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement