Problem with foreach on update/save

Published 6 days ago by petritr

I have the following array on dd():

array:2 [
  "data" => array:2 [
    0 => array:2 [
      "name" => "Hub Name"
      "value" => "100"
    ]
    1 => array:2 [
      "name" => "Hub Two"
      "value" => "200"
    ]
  ]
  "hub_id" => "1"
]

I will get invalid foreach:

        foreach ($hubs as $hub) {
            foreach($hub as $values) {

                $hub = Hub::find($hub_id);

                $hub->name = $values["value"];
                $hub->save();
            }
        }

The error: "Invalid argument supplied for foreach()"

What im doing wrong ?

martinbean

@petritr The error should also give a line number. We can’t see what the problem is because you’ve given us two foreach statements in your example—it could be either one of them.

petritr

im trying to update the model but still i have problem.

foreach ($Hubs as $hub) {
            foreach ($hub as $value) {
                $array[] = $value['value'];
            }

            foreach ($array as $arr) {
            $hub = Hub::find($hub_id);
            $hub->name = $arr;
            $hub->save();
            }
        }

i get an error converting string to array, this is on my nerves now :) I got the problem here $hub->name = $arr;

zion
zion
6 days ago (26,510 XP)

What are you trying to do? You're looping through an array just to add it to another and loop through that again.

It seems you're making things overly complex.

petritr

I need to update the database, i got that array and i need to update the mode with that array, any easy smart way ?

tykus
tykus
6 days ago (657,630 XP)

The last item hub_id in the array does not have an array value, so that will break the inner foreach:

$hubs = [
  "data" => [
    0 => [
      "name" => "Hub Name",
      "value" => "100",
    ],
    1 => [
      "name" => "Hub Two",
      "value" => "200",
    ]
  ],
  "hub_id" => "1"
]

foreach ($hubs['data'] as $hub) {
    foreach($hub as $values) {
        $hub = Hub::find($hubs['hub_id']);

        $hub->name = $values["value"];
        $hub->save();
    }
}
petritr

@tykus with $hubs['data'] i would be directly one level deeper in the array we wont need the second array.

As it is now i will get:

message: "Illegal string offset 'value'"

Im stuck here no ideas ha!

livesinspb
 foreach ($hubs as $hub) {
            foreach($hub as $values) {

                $hub = Hub::find($hub_id);

                $hub->name = $values["value"];
                $hub->save();
            }
        }

This is not correctly code. You don't use second foreach.

 foreach ($hubs['data'] as $hub)
  {
    $hubForSave = Hub::find($hubs['hub_id']);
    $hubForSave->name = $hub["value"];
    $hubForSave->save();
  }

Here you are!

petritr

If i do this then only the last value would be saved. If i have 2 inputs the last value would be taken.

dd($hub["value"];) will output also only the last input value.

How can i save all values not only the last.

petritr

I can do this if i check all inputs individually like:

$('[name="name"]').map(function () {
        return $(this).val();
    }).get();

But when i use .serializeArray()i cant pass the correct data from the foreach to the database, do i have to have the same name inputs as the database columns or ? I don't get this quite yet!

livesinspb

Hm...

If i do this then only the last value would be saved. If i have 2 inputs the last value would be taken.

No, this code is working how you need.

But every time save $hub['value'] last value.

Because you Primary Key for get is

$hubs['hub_id']

this value every time equals 1 !

Then you need another condition for save record.

petritr

Hmm i don't realy understand what you say, i have the problem with saving the data, i get the correct data but i cant save it.

Cronix
Cronix
5 days ago (727,800 XP)

This code makes no sense. Take your array:

array:2 [
  "data" => array:2 [
    0 => array:2 [
      "name" => "Hub Name"
      "value" => "100"
    ]
    1 => array:2 [
      "name" => "Hub Two"
      "value" => "200"
    ]
  ]
  "hub_id" => "1"
]

There is only one hub_id. It has an id of 1.

Then in the loop, you are getting the hub by that same id over and over.

$hub = Hub::find($hub_id);

and saving the "value" from the other array. So essentially, you are overwriting the same hub each time. Your code comes out to.

// loop 1
$hub = Hub::find(1); // get hub with id 1
$hub->name = "100"; // save 100 in name field
$hub->save();

// loop 2
$hub = Hub::find(1); // get hub with id of 1
$hub->name = "200"; // save 200 in name field
$hub->save();

So first you load hub with id of 1 and save "100" to the name field.

Then on the next iteration of the loop, you (again) load hub with id of 1 and save "200" to the name field, overwriting the previous value. So hub with id of 1 will only contain whatever the last value was in the loop.

I think you need to explain a bit more about what you're actually trying to do as it's hard to tell with your code.

petritr

I mean the hub_id in this case was one, this will be dynamic every time will be different ID of course. I past the array just for visualization what kind of array i have.

My question is how can i save the data in each column correctly when i return an array from serializeArray ? If i separate the array manually then i can save the right value for the right column.

My question is when i get all from data(collect and pass the inputs) with serializeArray how to save the data in correct column ?

Cronix
Cronix
5 days ago (727,800 XP)

I mean the hub_id in this case was one, this will be dynamic every time will be different ID of course. I past the array just for visualization what kind of array i have.

The point is the original array you showed only had a single hub_id, but were trying to save multiple values to it in the loop overwriting what you previously saved. It doesn't matter if the hub_id was 1 or 100, there is still only a single id. I still don't understand what you're trying to do since there is a single id and you didn't really explain it.

petritr

It will have an single hub_id of course, okay lets go to to my goal. How would you save the data when you would have the exactly the same data array as me ? That would be better question and better explanation for me.

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