Laravel 8 Project Home Services - Admin Delete Service
In this video, we will learn about deleting a service in the admin panel of our Laravel 8 project.
Let's see how we can delete a service.
Switch to the project and let's open the AdminServiceComponent class file.
Here, let's create a function for deleting the service:
public function deleteService($service_id)
{
$service = Service::find($service_id);
if($service->thumnail)
{
unlink('images/services/thubnails'. '/' . $service->thumbnail);
}
if($service->image)
{
unlink('images/services'. '/' . $service->image);
}
$service->delete();
session()->flash('message','Service has been deleted successfully!');
}
Now, go to the view file and add the following code before the form to show a confirmation message:
@if(Session::has('message'))
<div class="alert alert-success" role="alert"> {{Session::get('message')}} </div>
@endif
Next, call the deleteService function on the click event inside the table row by adding the following code:
<a href="#" onclick="confirm('Are you sure, you want to delete this service?') || event.stopImmediatePropagation()" style="margin-left:10px;" wire:click.prevent="deleteService({{$service->id}})"><i class="fa fa-times fa-2x text-danger"></i></a>
Now, let's check this. Go to the admin/all-services page and click on the delete link.
After confirming, the record will be deleted.
So, in this way, you can delete a service.