2026-02-06 14:56:56 +02:00
|
|
|
<?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,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-02-06 15:13:20 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the upvotes for the product.
|
|
|
|
|
*/
|
|
|
|
|
public function upvotes()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Upvote::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the comments for the product.
|
|
|
|
|
*/
|
|
|
|
|
public function comments()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Comment::class)->latest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the bookmarks for the product.
|
|
|
|
|
*/
|
|
|
|
|
public function bookmarks()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Bookmark::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a user has upvoted the product.
|
|
|
|
|
*/
|
|
|
|
|
public function isUpvotedBy(?User $user): bool
|
|
|
|
|
{
|
|
|
|
|
if (!$user) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->upvotes()->where('user_id', $user->id)->exists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a user has bookmarked the product.
|
|
|
|
|
*/
|
|
|
|
|
public function isBookmarkedBy(?User $user): bool
|
|
|
|
|
{
|
|
|
|
|
if (!$user) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->bookmarks()->where('user_id', $user->id)->exists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the upvotes count for the product.
|
|
|
|
|
*/
|
|
|
|
|
public function getUpvotesCountAttribute(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->upvotes()->count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the comments count for the product.
|
|
|
|
|
*/
|
|
|
|
|
public function getCommentsCountAttribute(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->comments()->count();
|
|
|
|
|
}
|
2026-02-06 14:56:56 +02:00
|
|
|
}
|