product submission
This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the products.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Product::with(['user', 'category'])
|
||||
->approved()
|
||||
->latest();
|
||||
|
||||
if ($request->has('category')) {
|
||||
$query->where('category_id', $request->category);
|
||||
}
|
||||
|
||||
$products = $query->paginate(12);
|
||||
$categories = Category::all();
|
||||
|
||||
return view('products.index', compact('products', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new product.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$categories = Category::all();
|
||||
return view('products.create', compact('categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created product in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
Gate::authorize('create-product');
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'required|string',
|
||||
'website_url' => 'required|url',
|
||||
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
]);
|
||||
|
||||
$validated['user_id'] = Auth::id();
|
||||
$validated['status'] = 'pending';
|
||||
|
||||
// Handle thumbnail upload
|
||||
if ($request->hasFile('thumbnail')) {
|
||||
$path = $request->file('thumbnail')->store('thumbnails', 'public');
|
||||
$validated['thumbnail'] = $path;
|
||||
}
|
||||
|
||||
$product = Product::create($validated);
|
||||
|
||||
return redirect()->route('products.show', $product)
|
||||
->with('success', 'Product submitted successfully! It will be reviewed by our moderators.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified product.
|
||||
*/
|
||||
public function show(Product $product)
|
||||
{
|
||||
// Only show approved products to non-owners
|
||||
if (!$product->isApproved() && $product->user_id !== Auth::id() && !Auth::user()?->hasAnyRole(['admin', 'moderator'])) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$product->load(['user', 'category']);
|
||||
|
||||
return view('products.show', compact('product'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified product.
|
||||
*/
|
||||
public function edit(Product $product)
|
||||
{
|
||||
Gate::authorize('edit-product', $product);
|
||||
|
||||
$categories = Category::all();
|
||||
return view('products.edit', compact('product', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified product in storage.
|
||||
*/
|
||||
public function update(Request $request, Product $product)
|
||||
{
|
||||
Gate::authorize('edit-product', $product);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'required|string',
|
||||
'website_url' => 'required|url',
|
||||
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
]);
|
||||
|
||||
// Handle thumbnail upload
|
||||
if ($request->hasFile('thumbnail')) {
|
||||
// Delete old thumbnail if exists
|
||||
if ($product->thumbnail) {
|
||||
Storage::disk('public')->delete($product->thumbnail);
|
||||
}
|
||||
|
||||
$path = $request->file('thumbnail')->store('thumbnails', 'public');
|
||||
$validated['thumbnail'] = $path;
|
||||
}
|
||||
|
||||
$product->update($validated);
|
||||
|
||||
return redirect()->route('products.show', $product)
|
||||
->with('success', 'Product updated successfully!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified product from storage.
|
||||
*/
|
||||
public function destroy(Product $product)
|
||||
{
|
||||
Gate::authorize('delete-product', $product);
|
||||
|
||||
// Delete thumbnail if exists
|
||||
if ($product->thumbnail) {
|
||||
Storage::disk('public')->delete($product->thumbnail);
|
||||
}
|
||||
|
||||
$product->delete();
|
||||
|
||||
return redirect()->route('products.index')
|
||||
->with('success', 'Product deleted successfully!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the product management dashboard for admin/moderator.
|
||||
*/
|
||||
public function manage(Request $request)
|
||||
{
|
||||
Gate::authorize('approve-product');
|
||||
|
||||
$query = Product::with(['user', 'category']);
|
||||
|
||||
// Filter by status
|
||||
if ($request->has('status') && in_array($request->status, ['pending', 'approved', 'rejected'])) {
|
||||
$query->byStatus($request->status);
|
||||
} else {
|
||||
$query->pending(); // Default to pending
|
||||
}
|
||||
|
||||
$products = $query->latest()->paginate(20);
|
||||
|
||||
$statusCounts = [
|
||||
'pending' => Product::pending()->count(),
|
||||
'approved' => Product::approved()->count(),
|
||||
'rejected' => Product::rejected()->count(),
|
||||
];
|
||||
|
||||
return view('products.manage', compact('products', 'statusCounts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve a product.
|
||||
*/
|
||||
public function approve(Product $product)
|
||||
{
|
||||
Gate::authorize('approve-product');
|
||||
|
||||
$product->approve();
|
||||
|
||||
return back()->with('success', 'Product approved successfully!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject a product.
|
||||
*/
|
||||
public function reject(Product $product)
|
||||
{
|
||||
Gate::authorize('reject-product');
|
||||
|
||||
$product->reject();
|
||||
|
||||
return back()->with('success', 'Product rejected.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'description',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the products for the category.
|
||||
*/
|
||||
public function products(): HasMany
|
||||
{
|
||||
return $this->hasMany(Product::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Product extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'slug',
|
||||
'description',
|
||||
'website_url',
|
||||
'thumbnail',
|
||||
'user_id',
|
||||
'category_id',
|
||||
'status',
|
||||
'approved_at',
|
||||
'rejected_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'approved_at' => 'datetime',
|
||||
'rejected_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Boot the model.
|
||||
*/
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($product) {
|
||||
if (empty($product->slug)) {
|
||||
$product->slug = static::generateUniqueSlug($product->title);
|
||||
}
|
||||
});
|
||||
|
||||
static::updating(function ($product) {
|
||||
if ($product->isDirty('title')) {
|
||||
$product->slug = static::generateUniqueSlug($product->title, $product->id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique slug from the title.
|
||||
*/
|
||||
protected static function generateUniqueSlug(string $title, ?int $ignoreId = null): string
|
||||
{
|
||||
$slug = Str::slug($title);
|
||||
$originalSlug = $slug;
|
||||
$counter = 1;
|
||||
|
||||
while (static::slugExists($slug, $ignoreId)) {
|
||||
$slug = $originalSlug . '-' . $counter;
|
||||
$counter++;
|
||||
}
|
||||
|
||||
return $slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a slug exists.
|
||||
*/
|
||||
protected static function slugExists(string $slug, ?int $ignoreId = null): bool
|
||||
{
|
||||
$query = static::where('slug', $slug);
|
||||
|
||||
if ($ignoreId) {
|
||||
$query->where('id', '!=', $ignoreId);
|
||||
}
|
||||
|
||||
return $query->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user that owns the product.
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the category that owns the product.
|
||||
*/
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include approved products.
|
||||
*/
|
||||
public function scopeApproved(Builder $query): Builder
|
||||
{
|
||||
return $query->where('status', 'approved');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include pending products.
|
||||
*/
|
||||
public function scopePending(Builder $query): Builder
|
||||
{
|
||||
return $query->where('status', 'pending');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include rejected products.
|
||||
*/
|
||||
public function scopeRejected(Builder $query): Builder
|
||||
{
|
||||
return $query->where('status', 'rejected');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to filter by status.
|
||||
*/
|
||||
public function scopeByStatus(Builder $query, string $status): Builder
|
||||
{
|
||||
return $query->where('status', $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the product's thumbnail URL.
|
||||
*/
|
||||
public function getThumbnailUrlAttribute(): ?string
|
||||
{
|
||||
if ($this->thumbnail) {
|
||||
return asset('storage/' . $this->thumbnail);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the product is pending.
|
||||
*/
|
||||
public function isPending(): bool
|
||||
{
|
||||
return $this->status === 'pending';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the product is approved.
|
||||
*/
|
||||
public function isApproved(): bool
|
||||
{
|
||||
return $this->status === 'approved';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the product is rejected.
|
||||
*/
|
||||
public function isRejected(): bool
|
||||
{
|
||||
return $this->status === 'rejected';
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve the product.
|
||||
*/
|
||||
public function approve(): void
|
||||
{
|
||||
$this->update([
|
||||
'status' => 'approved',
|
||||
'approved_at' => now(),
|
||||
'rejected_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject the product.
|
||||
*/
|
||||
public function reject(): void
|
||||
{
|
||||
$this->update([
|
||||
'status' => 'rejected',
|
||||
'rejected_at' => now(),
|
||||
'approved_at' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
@@ -111,4 +112,12 @@ class User extends Authenticatable
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the products for the user.
|
||||
*/
|
||||
public function products(): HasMany
|
||||
{
|
||||
return $this->hasMany(Product::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user