0

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?

1 Answer 1

1

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.