@extends('adminlte::page') @section('title', 'Manage Categories') @section('plugins.Datatables', true) @section('content_header')

Category Management

@can('create categories') Create New Category @endcan
@stop @section('content') @if ($message = Session::get('success')) @endif
@php $roots = $categories->whereNull('parent_id'); $children = $categories->whereNotNull('parent_id')->groupBy('parent_id'); $sortedCategories = collect(); $traverse = function ($items, $depth) use (&$traverse, $children, &$sortedCategories) { foreach ($items as $category) { $category->depth = $depth; $sortedCategories->push($category); if ($children->has($category->id)) { $traverse($children[$category->id], $depth + 1); } } }; $traverse($roots, 0); // Fallback for any orphans (should not happen in valid tree) $processedIds = $sortedCategories->pluck('id'); $orphans = $categories->whereNotIn('id', $processedIds); foreach($orphans as $orphan) { $orphan->depth = 0; $sortedCategories->push($orphan); } @endphp @foreach ($sortedCategories as $index => $category) @endforeach
No Name Code Parent Category Action
{{ $index + 1 }} @if($category->depth > 0) @endif {{ $category->name }} {{ $category->code }} {{ $category->parent ? $category->parent->name : 'N/A' }}
@can('edit categories') @endcan @can('delete categories')
@csrf @method('DELETE')
@endcan
@stop @section('css') @stop @section('js') @stop