admin and mod categories management
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CategoryManagementController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of categories.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Category::withCount('products');
|
||||
|
||||
// Search
|
||||
if ($request->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.');
|
||||
}
|
||||
}
|
||||
@@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user