admin and mod categories management

This commit is contained in:
Doar Mihai
2026-02-21 14:57:38 +02:00
parent 2c74b38ffc
commit 0f17570476
7 changed files with 552 additions and 3 deletions
@@ -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.');
}
}
+36
View File
@@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
class Category extends Model class Category extends Model
{ {
@@ -25,4 +26,39 @@ class Category extends Model
{ {
return $this->hasMany(Product::class); 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';
}
} }
@@ -0,0 +1,91 @@
@extends('layouts.app')
@section('content')
<div class="bg-gray-50 dark:bg-gray-900 min-h-screen">
<div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Header -->
<div class="mb-8">
<div class="flex items-center justify-between">
<div>
<h1 class="text-3xl font-bold text-gray-900 dark:text-white">Create Category</h1>
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400">Add a new product category</p>
</div>
<a
href="{{ route('categories.index') }}"
class="px-4 py-2 bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-md font-medium transition-colors"
>
Back to Categories
</a>
</div>
</div>
<!-- Form -->
<form action="{{ route('categories.store') }}" method="POST" class="space-y-6">
@csrf
<div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6">
<div class="space-y-4">
<!-- Name -->
<div>
<label for="name" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Category Name <span class="text-red-500">*</span>
</label>
<input
type="text"
name="name"
id="name"
value="{{ old('name') }}"
required
autofocus
class="w-full rounded-md border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-white shadow-sm focus:border-primary-500 focus:ring-primary-500 @error('name') border-red-500 @enderror"
placeholder="e.g., SaaS, Mobile Apps, Developer Tools"
>
@error('name')
<p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
A unique, descriptive name for the category
</p>
</div>
<!-- Description -->
<div>
<label for="description" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Description
</label>
<textarea
name="description"
id="description"
rows="4"
class="w-full rounded-md border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-white shadow-sm focus:border-primary-500 focus:ring-primary-500 @error('description') border-red-500 @enderror"
placeholder="Describe what types of products belong in this category..."
>{{ old('description') }}</textarea>
@error('description')
<p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
Help users understand what this category is for
</p>
</div>
</div>
</div>
<!-- Actions -->
<div class="flex items-center justify-between">
<a
href="{{ route('categories.index') }}"
class="px-4 py-2 bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-md font-medium transition-colors"
>
Cancel
</a>
<button
type="submit"
class="px-6 py-2 bg-primary-600 hover:bg-primary-700 text-white rounded-md font-medium transition-colors"
>
Create Category
</button>
</div>
</form>
</div>
</div>
@endsection
+120
View File
@@ -0,0 +1,120 @@
@extends('layouts.app')
@section('content')
<div class="bg-gray-50 dark:bg-gray-900 min-h-screen">
<div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Header -->
<div class="mb-8">
<div class="flex items-center justify-between">
<div>
<h1 class="text-3xl font-bold text-gray-900 dark:text-white">Edit Category</h1>
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400">Update category information</p>
</div>
<a
href="{{ route('categories.index') }}"
class="px-4 py-2 bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-md font-medium transition-colors"
>
Back to Categories
</a>
</div>
</div>
<!-- Form -->
<form action="{{ route('categories.update', $category) }}" method="POST" class="space-y-6">
@csrf
@method('PUT')
<div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6">
<div class="space-y-4">
<!-- Name -->
<div>
<label for="name" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Category Name <span class="text-red-500">*</span>
</label>
<input
type="text"
name="name"
id="name"
value="{{ old('name', $category->name) }}"
required
autofocus
class="w-full rounded-md border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-white shadow-sm focus:border-primary-500 focus:ring-primary-500 @error('name') border-red-500 @enderror"
placeholder="e.g., SaaS, Mobile Apps, Developer Tools"
>
@error('name')
<p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
A unique, descriptive name for the category
</p>
</div>
<!-- Current Slug Info -->
<div class="bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-md p-4">
<div class="flex items-start">
<svg class="h-5 w-5 text-blue-400 mt-0.5 mr-2" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"></path>
</svg>
<div class="flex-1">
<p class="text-sm text-blue-800 dark:text-blue-200">
Current slug: <code class="bg-blue-100 dark:bg-blue-900 px-2 py-0.5 rounded">{{ $category->slug }}</code>
</p>
<p class="text-xs text-blue-700 dark:text-blue-300 mt-1">
The slug will be automatically regenerated if you change the category name.
</p>
</div>
</div>
</div>
<!-- Description -->
<div>
<label for="description" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Description
</label>
<textarea
name="description"
id="description"
rows="4"
class="w-full rounded-md border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-white shadow-sm focus:border-primary-500 focus:ring-primary-500 @error('description') border-red-500 @enderror"
placeholder="Describe what types of products belong in this category..."
>{{ old('description', $category->description) }}</textarea>
@error('description')
<p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
@enderror
<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
Help users understand what this category is for
</p>
</div>
<!-- Category Stats -->
@if($category->products()->count() > 0)
<div class="bg-gray-50 dark:bg-gray-900/50 border border-gray-200 dark:border-gray-700 rounded-md p-4">
<h4 class="text-sm font-medium text-gray-900 dark:text-white mb-2">Category Usage</h4>
<p class="text-sm text-gray-600 dark:text-gray-400">
This category currently has <strong class="text-gray-900 dark:text-white">{{ $category->products()->count() }}</strong>
{{ Str::plural('product', $category->products()->count()) }} associated with it.
</p>
</div>
@endif
</div>
</div>
<!-- Actions -->
<div class="flex items-center justify-between">
<a
href="{{ route('categories.index') }}"
class="px-4 py-2 bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-md font-medium transition-colors"
>
Cancel
</a>
<button
type="submit"
class="px-6 py-2 bg-primary-600 hover:bg-primary-700 text-white rounded-md font-medium transition-colors"
>
Update Category
</button>
</div>
</form>
</div>
</div>
@endsection
+175
View File
@@ -0,0 +1,175 @@
@extends('layouts.app')
@section('content')
<div class="bg-gray-50 dark:bg-gray-900 min-h-screen">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Header -->
<div class="mb-8">
<div class="flex items-center justify-between">
<div>
<h1 class="text-3xl font-bold text-gray-900 dark:text-white">Category Management</h1>
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400">Manage all product categories</p>
</div>
<a
href="{{ route('categories.create') }}"
class="inline-flex items-center px-4 py-2 bg-primary-600 hover:bg-primary-700 text-white rounded-md font-medium transition-colors"
>
<svg class="h-5 w-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
</svg>
New Category
</a>
</div>
</div>
<!-- Success/Error Messages -->
@if(session('success'))
<div class="mb-6 bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 text-green-800 dark:text-green-200 px-4 py-3 rounded-lg">
{{ session('success') }}
</div>
@endif
@if(session('error'))
<div class="mb-6 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 text-red-800 dark:text-red-200 px-4 py-3 rounded-lg">
{{ session('error') }}
</div>
@endif
<!-- Search -->
<div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-4 mb-6">
<form method="GET" action="{{ route('categories.index') }}" class="flex gap-4">
<div class="flex-1">
<input
type="text"
name="search"
value="{{ request('search') }}"
placeholder="Search categories..."
class="w-full rounded-md border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-900 dark:text-white shadow-sm focus:border-primary-500 focus:ring-primary-500"
>
</div>
<div class="flex gap-2">
<button
type="submit"
class="px-4 py-2 bg-primary-600 hover:bg-primary-700 text-white rounded-md font-medium transition-colors"
>
Search
</button>
@if(request('search'))
<a
href="{{ route('categories.index') }}"
class="px-4 py-2 bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-md font-medium transition-colors"
>
Clear
</a>
@endif
</div>
</form>
</div>
<!-- Categories Table -->
<div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 overflow-hidden">
<div class="overflow-x-auto">
<table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
<thead class="bg-gray-50 dark:bg-gray-900">
<tr>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Category
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Slug
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Products
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Created
</th>
<th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Actions
</th>
</tr>
</thead>
<tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
@forelse($categories as $category)
<tr class="hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors">
<td class="px-6 py-4">
<div class="text-sm font-medium text-gray-900 dark:text-white">
{{ $category->name }}
</div>
@if($category->description)
<div class="text-sm text-gray-500 dark:text-gray-400 line-clamp-1">
{{ $category->description }}
</div>
@endif
</td>
<td class="px-6 py-4 whitespace-nowrap">
<code class="text-xs bg-gray-100 dark:bg-gray-700 text-gray-800 dark:text-gray-200 px-2 py-1 rounded">
{{ $category->slug }}
</code>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-primary-100 dark:bg-primary-900 text-primary-800 dark:text-primary-200">
{{ $category->products_count }} {{ Str::plural('product', $category->products_count) }}
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
{{ $category->created_at->format('M d, Y') }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
<div class="flex items-center justify-end gap-2">
<a
href="{{ route('categories.edit', $category) }}"
class="text-primary-600 dark:text-primary-400 hover:text-primary-900 dark:hover:text-primary-300"
>
Edit
</a>
<form
action="{{ route('categories.destroy', $category) }}"
method="POST"
onsubmit="return confirm('Are you sure you want to delete this category?{{ $category->products_count > 0 ? ' It has ' . $category->products_count . ' products.' : '' }}');"
class="inline"
>
@csrf
@method('DELETE')
<button
type="submit"
class="text-red-600 dark:text-red-400 hover:text-red-900 dark:hover:text-red-300"
>
Delete
</button>
</form>
</div>
</td>
</tr>
@empty
<tr>
<td colspan="5" class="px-6 py-12 text-center">
<div class="flex flex-col items-center">
<svg class="h-12 w-12 text-gray-400 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"></path>
</svg>
<p class="text-sm text-gray-500 dark:text-gray-400 mb-4">No categories found</p>
<a
href="{{ route('categories.create') }}"
class="inline-flex items-center px-4 py-2 bg-primary-600 hover:bg-primary-700 text-white rounded-md font-medium transition-colors"
>
Create your first category
</a>
</div>
</td>
</tr>
@endforelse
</tbody>
</table>
</div>
<!-- Pagination -->
@if($categories->hasPages())
<div class="px-6 py-4 border-t border-gray-200 dark:border-gray-700">
{{ $categories->links() }}
</div>
@endif
</div>
</div>
</div>
@endsection
@@ -137,7 +137,22 @@
<a href="{{ route('products.manage') }}" <a href="{{ route('products.manage') }}"
class="block px-4 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700" class="block px-4 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
> >
Manage products <div class="flex items-center">
<svg class="mr-2 h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"></path>
</svg>
Manage Products
</div>
</a>
<a href="{{ route('categories.index') }}"
class="block px-4 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
>
<div class="flex items-center">
<svg class="mr-2 h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"></path>
</svg>
Manage Categories
</div>
</a> </a>
@endif @endif
@@ -198,8 +213,12 @@
@endif @endif
@if(auth()->user()->hasAnyRole(['admin', 'moderator'])) @if(auth()->user()->hasAnyRole(['admin', 'moderator']))
<a href="{{ route('products.manage') }}" <a href="{{ route('products.manage') }}"
class="block pl-3 pr-4 py-2 border-l-4 text-base font-medium {{ request()->routeIs('products.manage') ? 'border-primary-500 text-primary-700 bg-primary-50' : 'border-transparent text-gray-500 hover:bg-gray-50 hover:border-gray-300 hover:text-gray-700' }}"> class="block pl-3 pr-4 py-2 border-l-4 text-base font-medium {{ request()->routeIs('products.manage') ? 'border-primary-500 text-primary-700 bg-primary-50 dark:bg-primary-900 dark:text-primary-200' : 'border-transparent text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 hover:border-gray-300 dark:hover:border-gray-600 hover:text-gray-700 dark:hover:text-gray-300' }}">
Manage Manage Products
</a>
<a href="{{ route('categories.index') }}"
class="block pl-3 pr-4 py-2 border-l-4 text-base font-medium {{ request()->routeIs('categories.*') ? 'border-primary-500 text-primary-700 bg-primary-50 dark:bg-primary-900 dark:text-primary-200' : 'border-transparent text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 hover:border-gray-300 dark:hover:border-gray-600 hover:text-gray-700 dark:hover:text-gray-300' }}">
Manage Categories
</a> </a>
@endif @endif
@endauth @endauth
+4
View File
@@ -3,6 +3,7 @@
use App\Http\Controllers\Admin\UserManagementController; use App\Http\Controllers\Admin\UserManagementController;
use App\Http\Controllers\AdminController; use App\Http\Controllers\AdminController;
use App\Http\Controllers\BookmarkController; use App\Http\Controllers\BookmarkController;
use App\Http\Controllers\CategoryManagementController;
use App\Http\Controllers\CommentController; use App\Http\Controllers\CommentController;
use App\Http\Controllers\ProductController; use App\Http\Controllers\ProductController;
use App\Http\Controllers\ProfileController; use App\Http\Controllers\ProfileController;
@@ -58,6 +59,9 @@ Route::middleware(['auth', 'moderator'])->group(function () {
Route::get('/products-manage', [ProductController::class, 'manage'])->name('products.manage'); Route::get('/products-manage', [ProductController::class, 'manage'])->name('products.manage');
Route::post('/products/{product}/approve', [ProductController::class, 'approve'])->name('products.approve'); Route::post('/products/{product}/approve', [ProductController::class, 'approve'])->name('products.approve');
Route::post('/products/{product}/reject', [ProductController::class, 'reject'])->name('products.reject'); Route::post('/products/{product}/reject', [ProductController::class, 'reject'])->name('products.reject');
// Category Management Routes (Moderators & Admins)
Route::resource('categories', CategoryManagementController::class);
}); });
// Admin Routes // Admin Routes