Laravel

How to delete a multilevel category and subcategory in Laravel – Part4?

In this tutorial of 91 TechSquare, we will learn how to delete a multilevel category and subcategory in Laravel. Earlier we have already learned how to create a multilevel category and subcategory in Laravel, how to display a multilevel category and subcategory in Laravel, and how to update a multilevel category and subcategory in Laravel.

We will use the same project with the same database table and model. This article is the enhancement of the previous article, in which we learned about how to update a multilevel category and subcategory in Laravel.

We will implement the functionality to delete a multilevel category and subcategory with the below simple steps. Please follow the below steps to accomplish this.

delete a multilevel category and subcategory in Laravel

1. Create a route to delete a multilevel category and subcategory in Laravel:

In this step, we will create a route to delete the categories. This route will handle the request to delete a multilevel category and subcategory.

Route::get('category/delete/{id}', [CategoryController::class, 'deleteCategory'])->name('deleteCategory');

We used a get request route to delete categories. We will just pass the id of category in this route. You can create according to your need as well.

We have already created the CategoryController in the previous article to create a multilevel category and subcategory in Laravel. CategoryController already includes at the top of the route file so do not be confused.

2. Create a method to delete category:

In this step, we will create a method deleteCategory() to handle the delete category route.

Use the following code into your controller for deleteCategory route.

public function deleteCategory($id)
{
    $category = Category::findOrFail($id);
    if(count($category->subcategory))
    {
        $subcategories = $category->subcategory;
        foreach($subcategories as $cat)
        {
            $cat = Category::findOrFail($cat->id);
            $cat->parent_id = null;
            $cat->save();
        }
    }
    $category->delete();
    return redirect()->back()->with('delete', 'Category has been deleted successfully.');
}

We are just getting the id of the category in this deleteCategory() method, then checking if the category exists or not. In case of category have child categories then we are making thri

3. Link URL for update category:

To update multilevel category and subcategory in Laravel, in this step we will use the URL of the update category inside the file of display categories. To link the URL we will use two files.

  • views/all-category.blade.php
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<section class="content" style="padding:50px 20%;">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <a class="add-new" href="{{Route('createCategory')}}">
                        <button class="btn btn-primary btn-xs">Add New Category</button>
                    </a>
                </div>
                @if(\Session::has('delete'))
                    <li class="alert alert-danger">{!! \Session::get('delete') !!}</li>
                @endif
                <div class="box-body">
                    <table class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>S.No.</th>
                            <th>Category Name</th>
                            <th>Category Slug</th>
                            <th>Parent Category</th>
                            <th>Action</th>
                        </tr>
                        </thead>
                        <tbody>
                        @if(isset($categories))
                            <?php $_SESSION['i'] = 0; ?>
                            @foreach($categories as $category)
                                <?php $_SESSION['i']=$_SESSION['i']+1; ?>
                                <tr>
                                    <?php $dash=''; ?>
                                    <td>{{$_SESSION['i']}}</td>
                                    <td>{{$category->name}}</td>
                                     <td>{{$category->slug}}</td>
                                    <td>
                                        @if(isset($category->parent_id))
                                            {{$category->subcategory->name}}
                                        @else
                                            None
                                        @endif

                                    </td>
                                   <td>                                  
                                        <a href="{{Route('editCategory', $category->id)}}">
                                            <button class="btn btn-sm btn-info">Edit</button>
                                        </a>
                                         <a href="{{Route('deleteCategory', $category->id)}}">
                                            <button class="btn btn-sm btn-danger">Delete</button>
                                        </a>
                                    </td>
                                </tr>
                            
                                 @if(count($category->subcategory))
                                     @include('sub-category-list',['subcategories' => $category->subcategory])
                                 @endif

                            @endforeach
                            <?php unset($_SESSION['i']); ?>
                        @endif
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>
  • views/sub-category-list.blade.php
<?php $dash.='-- '; ?>
@foreach($subcategories as $subcategory)
    <?php $_SESSION['i']=$_SESSION['i']+1; ?>
    <tr>
        <td>{{$_SESSION['i']}}</td>
        <td>{{$dash}}{{$subcategory->name}}</td>
        <td>{{$subcategory->slug}}</td>
        <td>{{$subcategory->parent->name}}</td>
       <td>
            <a href="{{Route('editCategory', $subcategory->id)}}">
                <button class="btn btn-sm btn-info">Edit</button>
            </a>
           <a href="{{Route('deleteCategory', $subcategory->id)}}">
                <button class="btn btn-sm btn-danger">Delete</button>
            </a>
        </td>
    </tr>
    @if(count($subcategory->subcategory))
        @include('sub-category-list',['subcategories' => $subcategory->subcategory])
    @endif
@endforeach

Now display categories page will be look like this:

delete a multilevel category and subcategory in Laravel

That’s it, now you can run the project by command php artisan serve, and visit the below route to delete a multilevel category and subcategory in Laravel:

http://127.0.0.1:8000/categories

Hopefully, You got an idea about how to delete a multilevel category and subcategory using a single table.

91 TechSquare

View Comments

Recent Posts

Top 5 Android Phones in India (2024): OnePlus Open, Samsung Galaxy S24 Ultra & More

Top 5 Android Phones in India Top 5 Android Phones in India for 2024: Unmatched…

3 months ago

Chapter 3: Understanding HTML Headings and Paragraphs

https://www.youtube.com/watch?v=QU0Hm7k_hWI Introduction: Previously, in Chapter 2, we discussed html elements and structure. In this chapter,…

4 months ago

Chapter 2: HTML Elements and Structure

https://youtu.be/HihwZMLuXGY Introduction: Welcome back to our HTML course! In the last chapter, we explored the…

4 months ago

Mastering Laravel Eloquent ORM: Easy Guide for Beginners

Introduction Mastering Laravel Eloquent ORM: Easy Guide for Beginners Laravel, one of the most popular…

4 months ago

Chapter 1: Introduction to HTML Basics

https://youtu.be/nPljiJtfBUQ In this chapter We will understand What HTML Is Understanding What HTML Is: Introduction:…

12 months ago

The SOLID principles: how to use them in Laravel to write better code

The SOLID principles are a set of guidelines for writing clean and maintainable code that…

2 years ago

This website uses cookies.