Rainieren
1 year ago
158
1
Laravel

Laravel - Delete user with anchor tag

Posted 1 year ago by Rainieren

Hello, I'm trying to make a function that deletes a user and all their content that they've posted on the website. I want to use an anchor tag for this. Currently, my code looks like this

<td class="text-center">
      <a href="{{ route('deleteUser', ['id' => $user->id]) }}" class="mr-3">
       {{ method_field('DELETE') }}
         <i class="fa fa-trash-o fa-font-24" style="color: red;"></i>
     </a>
     <a href="{{ route('editUsers', ['id' => $user->id]) }}">
         <i class="fa fa-edit fa-font-24" style="color: limegreen;"></i>
     </a>
 </td>

The update anchor tag works just fine but the delete button gives me an error which is:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

I've put a 'dd' in the controller but it does not reach that. What is going on? Can I even use an anchor tag for this?

The route I use looks like this:

Route::delete('/adminpanel/gebruikers/delete/{id}', '[email protected]')->name('deleteUser');

And the controller looks like this:

public function deleteUser($id)
    {
        dd($id);
        $user = User::find($id);

        $user->posts()->delete();
        $user->replies()->delete();

        $user->delete();

        return back()->with('flash', $user->firstname, '`s account is verwijderd.');
    }

Thanks in advance!

Please sign in or create an account to participate in this conversation.