Laravel E-Commerce Project - Admin Show Subcategories With Category

In this video, we will learn how to display subcategories with their respective categories in the admin panel.

Let's explore how to achieve this. First, switch to the project and open the Subcategory.php model file. Create a new function here:


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Subcategory extends Model
{
use HasFactory;

public function category()
{
$this->belongsTo(Category::class);
}
}

Next, open the Category.php model file and create another function:


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
use HasFactory;

protected $table = "categories";

public function subCategories()
{
return $this->hasMany(Subcategory::class,'category_id');
}
}

Now, open the admin-categories-components.blade.php view file. Inside the table, make the following changes and add some CSS styles:


<div>
<style>
nav svg{
height: 20px;
}
nav .hidden{
display: block !important;
}
.sclist{
list-style: none;
}
.sclist li{
line-height: 33px;
border-bottom: 1px solid #ccc;
}
.slink i{
font-size:16px;
margin-left:12px;
}
</style>
<div class="container" style="padding:30px 0;">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<div class="row">
<div class="col-md-6">
All Categories
</div>
<div class="col-md-6">
<a href="{{route('admin.addcategory')}}" class="btn btn-success pull-right">Add New</a>
</div>
</div>
</div>
<div class="panel-body">
@if(Session::has('message'))
<div class="alert alert-success" role="alert">{{Session::get('message')}}</div>
@endif
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Category Name</th>
<th>Slug</th>
<th>Sub Category</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach ($categories as $category)
<tr>
<td>{{$category->id}}</td>
<td>{{$category->name}}</td>
<td>{{$category->slug}}</td>
<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>
<td>
<a href="{{route('admin.editcategory',['category_slug'=>$category->slug])}}"><i class="fa fa-edit fa-2x"></i></a>
<a href="#" onclick="confirm('Are you sure, You want to delete this category?') || event.stopImmediatePropagation()" wire:click.prevent="deleteCategory({{$category->id}})" style="margin-left:10px; "><i class="fa fa-times fa-2x text-danger"></i></a>
</td>
</tr>
@endforeach
</tbody>
</table>
{{$categories->links()}}
</div>
</div>
</div>
</div>
</div>
</div>

That's it! Let's test the functionality.

Switch to the browser and refresh the page. You will see the categories and their subcategories displayed together.

In this way, you can easily display categories and subcategories in the admin panel.