product submission

This commit is contained in:
2026-02-06 14:56:56 +02:00
parent b2fd2a5eac
commit 2e1ce0d93b
15 changed files with 1673 additions and 0 deletions
+28
View File
@@ -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);
}
}
+200
View File
@@ -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,
]);
}
}
+9
View File
@@ -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);
}
}