Skip to content
Advertisement

Laravel, store multiple variables from array into DB

Im not sure if the question title is correct but what I wanna do is clear in the example below I have 3 arrays and I want to store data in one table

the table structure is like this

table name: tour_transports

id

transport_id

transport_track

transport_note

arrays coming from blade

  "transport_id" => array:2 [
    1 => "1"
    5 => "5"
  ]
  
  
  "transport_track" => array:3 [
    1 => " from airport to the hotel"
    3 => null
    5 => " be ready at 4:pm"
  ]
  
  
  "transport_note" => array:3 [
    1 => "from hotel to the beach"
    3 => null
    5 => " bring ur swiming suite"
  ]

so as you see the ids are 1 and 5 so I wanna store two rows here

id = 1,transport_id = 1 , transport_track = from airport to the hotel , transport_note = be ready at 4:pm

id = 2,transport_id = 5, transport_track = from hotel to the beach , transport_note = bring ur swiming suite

I tried to do this but can’t get it correct

 if(!empty($request->transport_id))
     {
       foreach ($request->transport_id as $key => $transport_id) {
         
         foreach ($request->transport_track as $key => $track) {

           foreach ($request->transport_note as $key => $transport_note) {
       

        TourTransport::create([
        'transport_id' =>$transport_id,
        'transport_track' =>$track[$transport_id],
        'transport_note' =>$transport_note[$transport_id]
                               ]);

              }
         
          }
       }
     }

Advertisement

Answer

You can use this code to achieve your results:

$data = $request->only('transport_id', 'transport_track', 'transport_note');

foreach ($data['transport_id'] as $key => $transportId) {
    TourTransport::create([
        'transport_id' => $transportId,
        'transport_track' => $data['transport_track'][$key],
        'transport_note' => $data['transport_note'][$key],
    ]);
}

Now $results contains the required data. However the better solution might be to configure your html form to send related data in single array entry, Like:

<input type="text" name="transports[0][transport_id]">
<input type="text" name="transports[0][transport_track]">
<input type="text" name="transports[0][transport_note]">

<input type="text" name="transports[1][transport_id]">
<input type="text" name="transports[1][transport_track]">
<input type="text" name="transports[1][transport_note]">

Which you can also use Javascript/php loops to generate the form inputs. Then you will receive form data in server side in well formed structure like:

"transports" => array:2 [
   [
       'transport_id' => 1,
       'transport_track' => 'from airport to the hotel',
       'transport_note' => 'be ready at 4:pm',
   ],
   [
       'transport_id' => 5,
       'transport_track' => 'from hotel to the beach',
       'transport_note' => ' bring ur swiming suite',
   ],
]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement