diff --git a/app/Http/Controllers/CategoryManagementController.php b/app/Http/Controllers/CategoryManagementController.php new file mode 100644 index 0000000..d048818 --- /dev/null +++ b/app/Http/Controllers/CategoryManagementController.php @@ -0,0 +1,104 @@ +filled('search')) { + $search = $request->search; + $query->where(function ($q) use ($search) { + $q->where('name', 'like', "%{$search}%") + ->orWhere('description', 'like', "%{$search}%"); + }); + } + + $categories = $query->latest()->paginate(20); + + return view('categories.index', compact('categories')); + } + + /** + * Show the form for creating a new category. + */ + public function create() + { + return view('categories.create'); + } + + /** + * Store a newly created category. + */ + public function store(Request $request) + { + $validated = $request->validate([ + 'name' => ['required', 'string', 'max:255', 'unique:categories,name'], + 'description' => ['nullable', 'string', 'max:1000'], + ]); + + // Generate unique slug + $validated['slug'] = Category::generateUniqueSlug($validated['name']); + + Category::create($validated); + + return redirect()->route('categories.index') + ->with('success', 'Category created successfully.'); + } + + /** + * Show the form for editing a category. + */ + public function edit(Category $category) + { + return view('categories.edit', compact('category')); + } + + /** + * Update the specified category. + */ + public function update(Request $request, Category $category) + { + $validated = $request->validate([ + 'name' => ['required', 'string', 'max:255', Rule::unique('categories')->ignore($category->id)], + 'description' => ['nullable', 'string', 'max:1000'], + ]); + + // Regenerate slug if name changed + if ($category->name !== $validated['name']) { + $validated['slug'] = Category::generateUniqueSlug($validated['name'], $category->id); + } + + $category->update($validated); + + return redirect()->route('categories.index') + ->with('success', 'Category updated successfully.'); + } + + /** + * Remove the specified category. + */ + public function destroy(Category $category) + { + // Check if category has products + if ($category->products()->count() > 0) { + return redirect()->route('categories.index') + ->with('error', "Cannot delete category '{$category->name}' because it has {$category->products()->count()} products. Please reassign or delete those products first."); + } + + $category->delete(); + + return redirect()->route('categories.index') + ->with('success', 'Category deleted successfully.'); + } +} diff --git a/app/Models/Category.php b/app/Models/Category.php index 0bbfea0..157b80e 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Support\Str; class Category extends Model { @@ -25,4 +26,39 @@ class Category extends Model { return $this->hasMany(Product::class); } + + /** + * Generate a unique slug for the category based on its name. + */ + public static function generateUniqueSlug(string $name, ?int $excludeId = null): string + { + $slug = Str::slug($name); + $originalSlug = $slug; + $count = 1; + + // Keep appending numbers until we find a unique slug + $query = static::where('slug', $slug); + if ($excludeId) { + $query->where('id', '!=', $excludeId); + } + + while ($query->exists()) { + $slug = $originalSlug . '-' . $count; + $count++; + $query = static::where('slug', $slug); + if ($excludeId) { + $query->where('id', '!=', $excludeId); + } + } + + return $slug; + } + + /** + * Get the route key for the model. + */ + public function getRouteKeyName(): string + { + return 'slug'; + } } diff --git a/resources/views/categories/create.blade.php b/resources/views/categories/create.blade.php new file mode 100644 index 0000000..a210afd --- /dev/null +++ b/resources/views/categories/create.blade.php @@ -0,0 +1,91 @@ +@extends('layouts.app') + +@section('content') +
Add a new product category
+Update category information
+Manage all product categories
+| + Category + | ++ Slug + | ++ Products + | ++ Created + | ++ Actions + | +
|---|---|---|---|---|
|
+
+ {{ $category->name }}
+
+ @if($category->description)
+
+ {{ $category->description }}
+
+ @endif
+ |
+
+
+ {{ $category->slug }}
+
+ |
+ + + {{ $category->products_count }} {{ Str::plural('product', $category->products_count) }} + + | ++ {{ $category->created_at->format('M d, Y') }} + | +
+
+
+ Edit
+
+
+
+ |
+
|
+
+
+
+ No categories found + + Create your first category + + |
+ ||||