In this video we are going to learn about Admin Show Subcategories With Category.
So lets see how can we Show Subcategories With Category.
Switch to the project and lets open Subcategory.php model.
Now create a 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);
}
}


Now lets open Category.php Model and here create another funciton.

<?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 lets open the admin-categories-components.blade.php view file.
Now inside the table make the following changes and also add some css.

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


Now its done so lets check it.
So switch to the browser and refresh the page and here you can see the category and their subcategories.
So in this way you can show the categories and subcategories.