Reputation:
I have a vue template and I'm trying to access the pets
array.
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
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