Skip to content
Advertisement

Laravel destroy nested resource

I have a resource name OrganizationEmailDomain

JavaScript

and able to index it just fine

enter image description here

In that view, when clicking in the three dots there’s the destroy part

JavaScript

If in that index view I echo $email_domains (<?php echo $email_domains;?>) then I get as expected (note that the following was before the images were added, so the records don’t match the images)

JavaScript

When I try to destroy

JavaScript

that dd($emailDomain) returns this

dd($emailDomain);

As langbox states

(…) hasMany relationship (…) so it returns a Collection always

So, inspired in this answer from Dan, just substituted

JavaScript

with the following

JavaScript

given that in Organization model I have

JavaScript

What’s the problem here?

It always deletes the record that is in the row above.

For example, let’s delete DDDDDDDDDDDDDDDDDDD

enter image description here

this deleted the first row, AAAAAAAAAAAAAAAAA

enter image description here

If I then try to delete BBBBBBBBBBBBBBB (which now became the first row) it’ll delete just fine.


For reference, here’s the index.blade.php where the form I shared previously is at

JavaScript

Advertisement

Answer

I’ve changed the route to shallow nesting

JavaScript

then the form to

JavaScript

and the destroy() to

JavaScript

and now it works fine.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement