Skip to content
Advertisement

How do delete single row in pivot table with laravel

I want to delete a single row of data in my pivot table. I don’t get any error but when try to click on the button. It did not redirect me to anywhere so the delete function is not performed.

pivot table

In the picture above I want to delete the highlighted id for user_id = 3 My scenario is that the user suddenly can’t make it to even_id = 6 so the user wants to delete/unjoined the event.

route

JavaScript

blade

JavaScript

controller

JavaScript

Event model

JavaScript

user model

JavaScript

Advertisement

Answer

I am adjusting your controller method to use Route Model Binding for simplicity:

JavaScript

As stated in the comments you need to adjust your route to Route::delete if you want to use the DELETE HTTP method that your form is spoofing via the @method('DELETE') blade directive.

Side note:

Auth::user() returns a User instance so you don’t need to query for it again, in your storeEventUser method:

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