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

/
/
/
1334 Views

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
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
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.

3 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar