Home > Enterprise >  Delete user by id on table user can't work
Delete user by id on table user can't work

Time:09-13

I want to delete my user... by id on table users

Form :

<form action="/dashboard/crud/{{ $user->id }}" method="post" >
@csrf
@method('delete')
<button  onclick="return confirm ('Hapus?')"><span data-feather="x-circle"></span></button>
</form>

Route:

Route::resource('/dashboard/crud', DashboardUserController::class)->middleware('auth');

Controller

public function destroy(User $user)
    {
        User::destroy($user->id);
        return redirect('/dashboard/crud'); with('success', 'data telah terhapus! ');
    }

Did I do something wrong? Please help...

CodePudding user response:

Your route is not correct

Route::resource('/dashboard/crud', DashboardUserController::class)->middleware('auth');

change this to

Route::post('/dashboard/crud/{user}', DashboardUserController::class)->middleware('auth');

https://laravel.com/docs/9.x/routing#required-parameters

Also delete this line @method('delete')

CodePudding user response:

The above mentioned process is correct. But in my opinion when you submit a form with POST method you should not send any value via Route/URL.

CodePudding user response:

Try this

<form action="/dashboard/crud/{{ $user->id }}" method="post" >
@csrf
@method('DELETE')
<button type="submit"  onclick="return confirm ('Hapus?')"><span data-feather="x-circle"></span></button>
</form>
  • Related