user9555022
user9555022

Reputation:

Access array for update Laravel + vue

I have a vue template and I'm trying to access the pets array.

enter image description here

This is my code

foreach ($request->pets as $pet) {
    $pet = $client->pets()->find($pet)->first();
    $pet->name = request('pets[].name');
    $pet->update();
}

If I hardcode a name, it updates. How do I access pet objects?

Upvotes: 0

Views: 60

Answers (1)

thisiskelvin
thisiskelvin

Reputation: 4202

You are confusing your loop here, setting both the foreach() variable as $pet, and the Pet model as $pet.

Try:

foreach ($request->pets as $pet) {
    $p = $client->pets()->find($pet)->first();
    $p->name = $pet['name'];
    $p->save();
}

Also, you can use the ->save() method here instead of ->update().

Upvotes: 1

Related Questions