update inputs with same name repeated

Published 1 month 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.

Best Answer (As Selected By petritr)
tykus

Loop over the array:

foreach ($devices[‘device_id’] as $key => $value) {
    $id = $devices[‘device_id’][$key];
    $name= $devices[‘device_name’][$key];
    $type = $devices[‘device_type’][$key];
    $modul = $devices[‘device_modul’][$key];

    // do your update
}
petritr

Any help for me ?

tykus
tykus
1 month ago (695,880 XP)

Loop over the array:

foreach ($devices[‘device_id’] as $key => $value) {
    $id = $devices[‘device_id’][$key];
    $name= $devices[‘device_name’][$key];
    $type = $devices[‘device_type’][$key];
    $modul = $devices[‘device_modul’][$key];

    // do your update
}
petritr

@tykus can you post also the update method?

When i do dd($id);i get "Use of undefined constant ‘device_id’ - assumed '‘device_id’' (this will throw an Error in a future version

Im confused :/

tykus
tykus
1 month ago (695,880 XP)

You must've forgotten to put the device_id key in quotation marks.

petritr

Okay i see changed to double quotes, on dd again i get device_id 1

Should i got back two id?

Yorki
Yorki
1 month ago (44,100 XP)
$keys = array_keys($devices);

for ($i = 0; $i < count($devices['device_id']); $i++) {
    $device = [];

    foreach ($keys as $key) {
        $device[$key] = $devices[$key][$i];
    }

    Device::update($device['device_id'], $device);
}
petritr

@Yorki I got back "Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically"

Yorki
Yorki
1 month ago (44,100 XP)

@petritr What laravel version are you using? Try this way:

$keys = array_keys($devices);

for ($i = 0; $i < count($devices['device_id']); $i++) {
    $device = [];

    foreach ($keys as $key) {
        $device[$key] = $devices[$key][$i];
    }

    // I assume you have all devices in database already
    $deviceModel = Device::find($device['device_id']);
    $deviceModel->fill($device);
    $deviceModel->save();
}
petritr

Why i get alway only one device_id even in normal foreach loop and here both examples will return only one device_id i got get it.

tykus
tykus
1 month ago (695,880 XP)

In @Yorki 's example, change the query:

 Device::findOrFail($device['device_id'])->update($device);
petritr

Now i get "Creating default object from empty value" both versions, im using laravel 5.6.

Uggghhhhh...

tykus
tykus
1 month ago (695,880 XP)

You should findOrFail - otherwise your query cannull which you are then attempting to assign properties/call methods on.

petritr

Yes i did use it findOrFail and i got that error Creating default object from empty value

on dd i get only one array i don't get both i don't understand this. Why?

tykus
tykus
1 month ago (695,880 XP)

Can you show us your full controller method as it is right now?

petritr

Here it is:

        $devices = $request->all();

        //dd($devices); this will return 2 arrays

        $keys = array_keys($devices);

        for ($i = 0; $i < count($devices['device_id']); $i++) {
            $device = [];

            foreach ($keys as $key) {
                $device[$key] = $devices[$key][$i];
            }
            //dd($device); this will return only one
            Device::findOrFail($device['device_id'])->update($device);
        }

Note: i have two dd with small explanation what i will get back from dd

first dd:

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"
  ]
  ....
]

second dd:

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

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