tykus
tykus
4 weeks ago (690,060 XP)

Can you dd($keys) and show us?

petritr

I get this:

array:6 [
  0 => "device_id"
  1 => "device_name"
  2 => "device_type"
  3 => "device_modul_1"
  ...
]

dd after for :

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

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

Any idea why i don't get both arrays when i loop through them? I don't get this! :/

Yorki
Yorki
4 weeks ago (44,100 XP)

@petrir Because of for loop. I've made it $device contains only one device per iteration so you get all attributes to update per device. You might check this by replacing dd with var_dump and die after for loop. You will see that loop is processing each device separately. Wasn't this your goal?

petritr

My goal is to update all inputs that are returned from the array @Yorki If i get back 2 different device_id then update both (with the rest of the data of course), if one update one, if four update for.

Yorki
Yorki
4 weeks ago (44,100 XP)

This is exactly what this code does. You have something wrong somewhere else. Where does error points to? File and line, paste it.

petritr

im using laravel 5.6 i have tired both version that you send i got errors for both:

            $deviceModel = Device::find($device['device_id']);
            $deviceModel->fill($device);
            $deviceModel->save();

error: "Creating default object from empty value"

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

error: "Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically"
Yorki
Yorki
4 weeks ago (44,100 XP)

@petrir Paste stacktrace too.

petritr

okay:

"ErrorException"
file
:
"/var/www/dir/app/app/Http/Controllers/DevicesController.php"
line
:
84
message
:
"Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically"
trace
:
[{file: "/var/www/dir/app/app/Http/Controllers/DevicesController.php", line: 84,…},…]
0
:
{file: "/var/www/dir/appy/app/Http/Controllers/DevicesController.php", line: 84,…}
1
:
{function: "update_devices", class: "oderentry\Http\Controllers\DevicesController", type: "->"}
2
:
{file: "/var/www/dir/app/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",…}
3
:
{,…}
petritr

Okay i got it i was a bit confuse but i got with @tykus loop, at the end not so difficult.

        foreach ($devices_update["device_id"] as $index => $device) {

            $id = ($devices_update["device_id"][$index]);
            $name = ($devices_update["device_name"][$index]);
            $type = ($devices_update["device_type"][$index]);
            $modul_1 = ($devices_update["device_modul_1"][$index]);
         ...

            $DeviceForSave = Device::find($id);

            $DeviceForSave->name = $name;
            $DeviceForSave->type = $type;
            $DeviceForSave->usage = $type;
            ...

            $DeviceForSave->save();

        }

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