Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use DELETE method in route with Laravel 5.4

Tags:

I'm working on a Laravel (v 5.4) project and i did the CRUD to manage categories. Currently, i can create a new category and i would be able to delete.

I created the view (with blade) to delete the categories :

<table class="table">   <thead>     <th>Name</th>     <th>Action</th>   </thead>   <tbody>     @foreach ($categories as $category)       <tr>         <td>$category->name</td>         <td>           <a href="{{ url('/categories', ['id' => $category->id]) }}">             <button class="btn btn-default">             Delete             </button>           </a>         </td>       </tr>     @endforeach   </tbody> </table> 

And in the routing file web.php, i wrote :

Route::delete('/categories/{id}', CategoryController@destroy); 

I have a controller CategoryController with a method destroy() who delete category and redirect to list of categories. But when i click on the button to delete, i get an error that explain this route is not define. If i replace Route::delete with Route::get it works. I think the url is called with GET but i would keep that for an other action.

I tried to replace the link with a form and "DELETE" as the value of "method" attribute but it didn't work.

How can i call url with DELETE method to catch it with Route::delete ?

Thanks in advance.

like image 397
Needlle Avatar asked May 22 '17 13:05

Needlle


People also ask

What is delete route in Laravel?

Step 1: Create Controller UserController by executing this command. Step 2: We can delete records in two ways. Second Method: The second way is to delete using the Laravel delete Function and User Model (Easy one). ->name( 'users.

How do I delete data in Laravel?

Laravel Eloquent Deleting You can delete data after writing it to the database. You can either delete a model instance if you have retrieved one, or specify conditions for which records to delete. To delete a model instance, retrieve it and call the delete() method: $user = User::find(1); $user->delete();

What is prefix in Laravel route?

Route PrefixesThe prefix method may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with admin : Route::prefix('admin')->group(function () { Route::get('/users', function () {


2 Answers

If you click on an url it will always be a GET method.

Since you wish to define it as DELETE, you should remake it into a post form and add

<input type="hidden" name="_method" value="delete" /> 

in it. Like replace:

<a href="{{ url('/categories', ['id' => $category->id]) }}">     <button class="btn btn-default">Delete</button> </a> 

with:

<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">     <input class="btn btn-default" type="submit" value="Delete" />     <input type="hidden" name="_method" value="delete" />     <input type="hidden" name="_token" value="{{ csrf_token() }}"> </form> 

Same goes for PUT request.

Since Laravel 5.1 method_field:

<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">     <input class="btn btn-default" type="submit" value="Delete" />     {!! method_field('delete') !!}     {!! csrf_field() !!} </form> 

Since Laravel 5.6 just with @ tag:

<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">     <input class="btn btn-default" type="submit" value="Delete" />     @method('delete')     @csrf </form> 
like image 52
Peon Avatar answered Sep 20 '22 06:09

Peon


For laravel 5.7 please look my example:

<form action="{{route('statuses.destroy',[$order_status->id_order_status])}}" method="POST">  @method('DELETE')  @csrf  <button type="submit">Delete</button>                </form> 
like image 34
devugur Avatar answered Sep 24 '22 06:09

devugur