petritr
159
6
Laravel

How can i save with one foreach loop object returned from ajax

Posted 8 months ago by petritr

I have the following object:

"device_id_1" => "100"
  "type_1" => "Some type"
  "name_1" => "Some Name"
  "modul_1_1" => "Modul 1"
  "device_id_2" => "200"
  "type_2" => "Other type"
  "name_2" => "Name"

in the controller i have the following:

$devices = $request->all();  // Will return to correct data
$devicesS = [];

        foreach ($devices as $index => $deviceId) {
            $devicesS[] = Device::create([
                'device_id' => $deviceId,
                'name' => $request->name_[$index],
                'type' => $request->type_[$index],
                'modul_1' => $request->modul_1_[$index],
            ]);
        }


My loop is not working at the moment, any ideas how to save object with one foreach loop?

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

Reply to

Use Markdown with GitHub-flavored code blocks.