tags management

This commit is contained in:
Doar Mihai
2026-02-21 16:12:57 +02:00
parent 898bddf614
commit 76d888760c
13 changed files with 850 additions and 2 deletions
+17 -2
View File
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Product;
use App\Models\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
@@ -84,7 +85,8 @@ class ProductController extends Controller
public function create()
{
$categories = Category::all();
return view('products.create', compact('categories'));
$tags = Tag::all();
return view('products.create', compact('categories', 'tags'));
}
/**
@@ -100,6 +102,8 @@ class ProductController extends Controller
'website_url' => 'required|url',
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
'category_id' => 'required|exists:categories,id',
'tags' => 'nullable|array',
'tags.*' => 'exists:tags,id',
]);
$validated['user_id'] = Auth::id();
@@ -113,6 +117,11 @@ class ProductController extends Controller
$product = Product::create($validated);
// Attach tags if provided
if ($request->has('tags')) {
$product->tags()->attach($request->tags);
}
return redirect()->route('products.show', $product)
->with('success', 'Product submitted successfully! It will be reviewed by our moderators.');
}
@@ -140,7 +149,8 @@ class ProductController extends Controller
Gate::authorize('edit-product', $product);
$categories = Category::all();
return view('products.edit', compact('product', 'categories'));
$tags = Tag::all();
return view('products.edit', compact('product', 'categories', 'tags'));
}
/**
@@ -156,6 +166,8 @@ class ProductController extends Controller
'website_url' => 'required|url',
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
'category_id' => 'required|exists:categories,id',
'tags' => 'nullable|array',
'tags.*' => 'exists:tags,id',
]);
// Handle thumbnail upload
@@ -171,6 +183,9 @@ class ProductController extends Controller
$product->update($validated);
// Sync tags (will remove old tags and add new ones)
$product->tags()->sync($request->tags ?? []);
return redirect()->route('products.show', $product)
->with('success', 'Product updated successfully!');
}
+106
View File
@@ -0,0 +1,106 @@
<?php
namespace App\Http\Controllers;
use App\Models\Tag;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
class TagController extends Controller
{
/**
* Display a listing of tags.
*/
public function index(Request $request)
{
$query = Tag::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}%");
});
}
$tags = $query->latest()->paginate(20);
return view('tags.index', compact('tags'));
}
/**
* Show the form for creating a new tag.
*/
public function create()
{
return view('tags.create');
}
/**
* Store a newly created tag.
*/
public function store(Request $request)
{
$validated = $request->validate([
'name' => ['required', 'string', 'max:255', 'unique:tags,name'],
'color' => ['required', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
'description' => ['nullable', 'string', 'max:1000'],
]);
// Generate unique slug
$validated['slug'] = Tag::generateUniqueSlug($validated['name']);
Tag::create($validated);
return redirect()->route('tags.index')
->with('success', 'Tag created successfully.');
}
/**
* Show the form for editing a tag.
*/
public function edit(Tag $tag)
{
return view('tags.edit', compact('tag'));
}
/**
* Update the specified tag.
*/
public function update(Request $request, Tag $tag)
{
$validated = $request->validate([
'name' => ['required', 'string', 'max:255', Rule::unique('tags')->ignore($tag->id)],
'color' => ['required', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
'description' => ['nullable', 'string', 'max:1000'],
]);
// Regenerate slug if name changed
if ($tag->name !== $validated['name']) {
$validated['slug'] = Tag::generateUniqueSlug($validated['name'], $tag->id);
}
$tag->update($validated);
return redirect()->route('tags.index')
->with('success', 'Tag updated successfully.');
}
/**
* Remove the specified tag.
*/
public function destroy(Tag $tag)
{
// Check if tag has products
if ($tag->products()->count() > 0) {
return redirect()->route('tags.index')
->with('error', "Cannot delete tag '{$tag->name}' because it is used by {$tag->products()->count()} products. Please remove it from those products first.");
}
$tag->delete();
return redirect()->route('tags.index')
->with('success', 'Tag deleted successfully.');
}
}
+9
View File
@@ -5,6 +5,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Str;
class Product extends Model
@@ -115,6 +116,14 @@ class Product extends Model
return $this->belongsTo(Category::class);
}
/**
* Get the tags for the product.
*/
public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class);
}
/**
* Scope a query to only include approved products.
*/
+65
View File
@@ -0,0 +1,65 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Str;
class Tag extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'slug',
'color',
'description',
];
/**
* Get the products that have this tag.
*/
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class);
}
/**
* Generate a unique slug for the tag 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';
}
}