Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel delete method not working

I've made a small laravel project but the delete method is nog working:

I use a resource controller my route is :

Route::resource('roles','Admin\RoleController');

in my view I have

 <form action="{{route('roles.destroy',$role->id)}}" style="display:inline">
    @method('delete')
    @csrf
    <button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
 </form>

But when i click the button it will show me the role ( = get method of the resource ) What am I doing wrong ?

like image 200
Jeme Avatar asked Jan 27 '26 18:01

Jeme


1 Answers

If you're using Laravel 5.1 or later

<form action="{{ route('roles.destroy', 'YOUR_ID') }}" method="POST">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>

If you're using Laravel 5.6 or later

<form action="{{ route('roles.destroy', 'YOUR_ID') }}" method="POST">
    @method('DELETE')
    @csrf
    <button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</form>

You can read more about method spoofing in Laravel Documentation.

like image 53
Shreeraj Avatar answered Jan 30 '26 11:01

Shreeraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!