petritr
1 year ago
106
25
Laravel

update inputs with same name repeated

Posted 1 year ago by petritr

I have the following in the view:

@foreach ($devices as $index => $device)

    <input type="text" name="device_id_update[]" value="{{ $device->device_id }}">
    <input type="text" name="device_name_update[]" value="{{ $device->name }}">
    <input type="text" name="device_type_update[]" value="{{ $device->type }}">
    <input type="text" name="device_modul_1_update[]" value="{{ $device->modul_1 }}">

@endforeach

The inputs are repeated, i need to update each input based on the device_id_update The array look like:

array:6 [
  "device_id" => array:2 [
    0 => "1"
    1 => "2"
  ]
  "device_name" => array:2 [
    0 => "Device 1"
    1 => "Device 2"
  ]
  "device_type" => array:2 [
    0 => "Type 1"
    1 => "Type 2"
  ]
  "device_modul_1" => array:2 [
    0 => "Modul 1"
    1 => "Modul 2"
  ]
]

I need to update each inputs based on the id in the controller i have:

$devices = $request->all();

    foreach ($devices['device_id'] as $device ) {
            $saveDevice = Device::find($device);
        }
        foreach ($devices['device_name'] as $device_name ) {
            $saveDevice->name = $device_name;
        }
    ....

This will update only the last last input, how can i update each input based on the id. This will grab only one device_id also, im stuck here.

Please sign in or create an account to participate in this conversation.