I am creating a system of categories and subcategories
Infinity.
For now I have managed to create two-level categories and subcategories eg.
CATEGORY_1
--SUBCATEGORY_1
CATEGORY_2
--SUBCATEGORY_2
I would like to create infinite levels. how can I do?
categories tables
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('parent_id')->default(0);
$table->string('name');
$table->boolean('status')->default(1);
$table->timestamps();
});
}
Models\Category
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
protected $fillable = [
'name',
'parent_id'
];
public function children()
{
return $this->hasMany('App\Models\Category', 'parent_id');
}
}
CategoryController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\Category;
class CategoryController extends Controller
{
public function index(){
$categories = Category::with('children')->where('parent_id', '=', 0)->get();
return view('backend.category.index')->with([
'categories' => $categories
]);
}
public function create(){
$categories = Category::with('children')->where('parent_id', '=', 0)->get();
return view('backend.category.create')->with([
'categories' => $categories
]);
}
public function store(Request $request)
{
$validatedData = $this->validate($request, [
'name' => 'required|min:3|max:255|string',
'parent_id' => 'sometimes|nullable|numeric',
'status' => 'required'
]);
Category::create($validatedData);
return redirect()->route('category.index')->withSuccess('You have successfully created a Category!');
}
}
index.blade.php
<ul class="list-group">
@foreach ($categories as $category)
<li class="list-group-item">
<div class="d-flex justify-content-between">
{{ $category->name }}
<div class="button-group d-flex">
<button type="button" class="btn btn-sm btn-primary mr-1 edit-category" data-toggle="modal">Edit</button>
<form action="#" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</div>
</div>
@if ($category->children)
<ul class="list-group mt-2">
@foreach ($category->children as $child)
<li class="list-group-item">
<div class="d-flex justify-content-between">
{{ $child->name }}
<div class="button-group d-flex">
<button type="button" class="btn btn-sm btn-primary mr-1 edit-category" data-toggle="modal">Edit</button>
<form action=" #" method="POST">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</div>
</div>
</li>
@endforeach
</ul>
@endif
</li>
@endforeach
</ul>