Laravel 8 Project Home Services - Admin Delete Service Category
In this video, we will learn about deleting a service category.
Let's see how we can delete a service category.
To do this, switch to the project and let's open the AdminServiceCategoryComponent class file.
Here, let's create a function for deleting a service category:
public function deleteServiceCategory($id)
{
$scategory = ServiceCategory::find($id);
if($ scategory ->image)
{
unlink('images/categories'.'/'.$ scategory >image);
}
$scategory->delete();
session()->flash('message','ServiceCategory has been deleted successfully!');
}
Now, go to the AdminCategoryComponent view file and here, let's create a link:
<a href="#"wire:click.prevent="deleteServiceCategory({{$scategory->id}})" style="margin-left:10px; "><i class="fa fa-times fa-2x text-danger"></i></a>
And inside this link, call the delete service category function and pass the parameter ID.
One more thing, add a confirmation before deleting the service category.
Here, before this wire, write:
<a href="#" onclick="confirm('Are you sure, You want to delete this category?') || event.stopImmediatePropagation()" wire:click.prevent="deleteServiceCategory({{$scategory->id}})" style="margin-left:10px; "><i class="fa fa-times fa-2x text-danger"></i></a>
Now, save this file and let's check it.
Switch to the browser and refresh the page.
Now, click on the delete link.
And here, you can see the message "Service category has been deleted successfully".
So, in this way, you can delete a service category.