Laravel 8 E-Commerce - Admin Delete Product
In this video, we will learn about how to delete a product.
Let's see how we can delete a product. First, open the AdminProductComponent.php class file and create a function for deleting the product.
Inside this file, add the following code:
public function deleteProduct($id)
{
$product = Product::find($id);
$product->delete();
session()->flash('message','Product has been deleted successfully!');
}
Next, open the admin-product-component.blade.php view file and add the delete link to the table:
<a href="#" wire:click.prevent="deleteProduct({{$product->id}})"><i class="fa fa-times fa-2x text-danger" style="margin-left:10px;"></i></a>
To display a success message, add an alert before the table:
@if(Session::has('message'))
<div class="alert alert-success" role="alert">{{Session::get('message')}}</div>
@endif
Now, everything is set up. Let's check it. Switch to the browser and refresh the page.
Delete any product by clicking on the delete link. You will see a message indicating that the product has been deleted.
So, in this way, you can delete a product. That's all about deleting a product.