Reputation: 91
i want to do seller can delete the product
<form action="{{ route('product.destroy'}}" method="post">
{{csrf_field()}}
{{method_field('DELETE')}}
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
this is in web.php
Route::get('/index', 'ProductController@index');//seller view all product
Route::get('/create', 'ProductController@create'); //seller create new product
Route::post('','ProductController@store')->name('product.store'); //store in database
Route::get('/edit/{id}','ProductController@edit'); // seller edit post
Route::put('edit/{id}','ProductController@update')->name('product.update'); //seller update
Route::delete('/{id}','ProductController@destroy')->name('product.destroy');//seller delete product
this is in ProductController
public function destroy($id)
{
$product= Product::find($id);
Storage::delete($product->image);
$product->delete();
return back()->withInfo('Product has been deleted');
}
HELP me please
Upvotes: 0
Views: 243
Reputation: 192
Pass the ID in the route in form tags,
{{ route('product.destroy', $product->id)}}
Also
$product= Product::find($id);
Storage::delete($product->image);
$product->delete();
Check if their is a $product, then delete it, otherwise you'll get an error.
Upvotes: 0
Reputation: 351
I think you are missing ID do it like
{{ route('product.destroy', $product->id)}}
Upvotes: 3
Reputation: 124
you can try removing the post method of the form cause you specify another one and depend on which version you are using you can try @method('DELETE')
@method('DELETE')
it just more elegant
Upvotes: 0