In this video, we will learn how to delete a subcategory in the admin panel.
Let's explore how to achieve this. First, switch to the project and open the Admin Category Component class file. Create a new function for deleting a subcategory:
public function deleteSubcategory($id)
{
$scategory = Subcategory::find($id);
$scategory->delete();
session()->flash('message','Subcategory has been deleted successfully!');
}
Next, go to the Admin-category-component.blade.php view file. Inside the subcategory foreach loop, create a link to delete the subcategory:
<td>
<ul class=`sclist`>
@foreach($category->subCategories as $scategory)
<li><i class=`fa fa-caret-right`></i> {{$scategory->name}}
<a href=`{{route('admin.editcategory',['category_slug'=>$category->slug,'scategory_slug'=>$scategory->slug])}}` class=`slink`><i class=`fa fa-edit`></i></a>
<a href=`#` onclick=`confirm('Are you sure, You want to delete this subcategory?') || event.stopImmediatePropagation()` wire:click.prevent=`deleteSubcategory({{$scategory->id}})` class=`slink`><i class=`fa fa-times text-danger`></i></a>
</li>
@endforeach
</ul>
</td>
Now, let's add some CSS styles for the subcategory. Inside the style tag, write the following code:
<style>
.sclist{
list-style: none;
}
.sclist li{
line-height: 33px;
border-bottom: 1px solid #ccc;
}
.slink i{
font-size:16px;
margin-left:12px;
}
</style>
That's it! Let's test the functionality.
Switch to the browser and refresh the page. You will see the delete link next to each subcategory. Click on the delete link to delete a subcategory.
A confirmation dialog box will appear. If you cancel, the subcategory will not be deleted. Click on the delete link again and then click `OK` to confirm the deletion.
After confirming, the subcategory will be deleted. In this way, you can easily delete a subcategory in the admin panel.






