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!');
}