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

8
  • Could you check storage/logs/laravel.log and see if there is any relevant error there? Commented Apr 15, 2018 at 9:59
  • produit is that variable spelled correctly? Commented Apr 15, 2018 at 10:00
  • what version of laravel is this? Commented Apr 15, 2018 at 10:01
  • DigitalDrifter , yes Commented Apr 15, 2018 at 10:03
  • laravel version 5.5 Commented Apr 15, 2018 at 10:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.