3

Just making a delete request from a form but not working. can you help please ?

<form method="POST" action="/products/{{ $produit->id }}"> 
    @csrf
    @method('DELETE')
    <button type="submit">delete</button>

here is my route:

Route::delete('/products/{del}',function($del){
    return $del.' deleted';
});

this give no errors, i just have a blank page

Barbell
  • 194
  • 1
  • 6
  • 19

1 Answers1

2

One of your problems could be that you spelt "product" wrong in your form. If that is not the issue, maybe try the following below.

In your routes file. Put the route I provided a few lines below in your web.php. If u don't already have a ProductController. You can easily create one by doing php artisan make:controller ProductController --resource in your console

Put this route in your web.php file to handle the request.

Route::delete('/products/{id}/delete', 'ProductController@destroy')->name('deleteProduct');

Now if u made the route correctly you could do something like this in your form'

<form method="POST" action="{{ route('deleteProduct', ['id' => $product->id]) }}">
  {{ csrf_field() }}
  {{ method_field('DELETE') }}

  <button type="submit">Delete</button>
</form>

This is passing the ID of the selected product to the route and into the destroy method of the productController. Then in your product controller (Assuming you have a product model) you could do $product = Product::find($id) and you can do whatever you want with the product.

I hope this is what you were looking for.

Rainier laan
  • 1,101
  • 5
  • 26
  • 63