Use the DELETE method along the route with Laravel 5.4

I am working on a Laravel project (v 5.4) and I have done CRUD for category management. Currently I can create a new category and I could delete.

I created a view (with a blade) to remove 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 web.php routing file, I wrote:

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

I have a CategoryController with a kill () method that removes a category and redirects to a list of categories. But when I click the button to delete, I get an error message explaining that this route is not defined. If I replace Route::deletewith Route::get, it works. I think url is being called with GET, but I would save this for another action.

I tried to replace the link with the form and "DELETE" as the value of the attribute "method", but it does not work.

url DELETE, Route::delete?

.

+11
3

URL, GET.

,

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

. :

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

:

<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>

PUT.

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>

Laravel 5.6 @:

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

, GET POST, . , . . .

<table class="table">
    <thead>
    <th>Name</th>
    <th>Action</th>
    </thead>
    <tbody>
    @foreach ($categories as $category)
        <tr>
            <td>$category->name</td>
            <td>
                <form action="/categories/{{ $category->id }}" method="post">
                    {{ method_field('delete') }}
                    <button class="btn btn-default" type="submit">Delete</button>
                </form>
            </td>
        </tr>
    @endforeach
    </tbody>
</table>
+3

For laravel 5.7, see my example:

<form action="{{route('statuses.destroy',[$order_status->id_order_status])}}" method="POST">
 @method('DELETE')
 @csrf
 <button type="submit">Delete</button>               
</form>
+3
source

Source: https://habr.com/ru/post/1677691/


All Articles