2026-02-06 14:56:56 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2026-10-17 15:56:58 +03:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2026-02-06 14:56:56 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2026-02-21 16:12:57 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2026-10-17 16:00:23 +03:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2026-02-06 14:56:56 +02:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
class Product extends Model
|
|
|
|
|
{
|
2026-10-17 15:56:58 +03:00
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Badge keys and their labels.
|
|
|
|
|
*/
|
|
|
|
|
public const BADGES = [
|
|
|
|
|
'free_tier' => 'Free tier',
|
|
|
|
|
'open_source' => 'Open source',
|
|
|
|
|
'eu_hosted' => 'EU hosted',
|
|
|
|
|
'eu_made' => 'EU made',
|
|
|
|
|
];
|
|
|
|
|
|
2026-02-06 14:56:56 +02:00
|
|
|
/**
|
|
|
|
|
* 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',
|
2026-10-17 15:56:58 +03:00
|
|
|
'free_tier',
|
|
|
|
|
'open_source',
|
|
|
|
|
'eu_hosted',
|
|
|
|
|
'eu_made',
|
2026-02-06 14:56:56 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'approved_at' => 'datetime',
|
|
|
|
|
'rejected_at' => 'datetime',
|
2026-10-17 15:56:58 +03:00
|
|
|
'free_tier' => 'boolean',
|
|
|
|
|
'open_source' => 'boolean',
|
|
|
|
|
'eu_hosted' => 'boolean',
|
|
|
|
|
'eu_made' => 'boolean',
|
2026-02-06 14:56:56 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-15 16:24:47 +02:00
|
|
|
/**
|
|
|
|
|
* Get the route key for the model.
|
|
|
|
|
*/
|
|
|
|
|
public function getRouteKeyName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'slug';
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 14:56:56 +02:00
|
|
|
/**
|
|
|
|
|
* 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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 16:00:23 +03:00
|
|
|
/**
|
|
|
|
|
* Get the product's screenshots in display order.
|
|
|
|
|
*/
|
|
|
|
|
public function screenshots(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(ProductScreenshot::class)->orderBy('position')->orderBy('id');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 16:02:27 +03:00
|
|
|
/**
|
|
|
|
|
* Get the product's pricing plans in display order.
|
|
|
|
|
*/
|
|
|
|
|
public function pricingPlans(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(PricingPlan::class)->orderBy('position')->orderBy('id');
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 14:56:56 +02:00
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 16:12:57 +02:00
|
|
|
/**
|
|
|
|
|
* Get the tags for the product.
|
|
|
|
|
*/
|
|
|
|
|
public function tags(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Tag::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 14:56:56 +02:00
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-07 14:36:33 +02:00
|
|
|
/**
|
|
|
|
|
* Scope a query to search products by title and description.
|
|
|
|
|
*/
|
|
|
|
|
public function scopeSearch(Builder $query, ?string $search): Builder
|
|
|
|
|
{
|
|
|
|
|
if (empty($search)) {
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $query->where(function ($subQuery) use ($search) {
|
|
|
|
|
$subQuery->where('title', 'like', '%' . $search . '%')
|
|
|
|
|
->orWhere('description', 'like', '%' . $search . '%');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to filter by category.
|
|
|
|
|
*/
|
|
|
|
|
public function scopeByCategory(Builder $query, ?int $categoryId): Builder
|
|
|
|
|
{
|
|
|
|
|
if (empty($categoryId)) {
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $query->where('category_id', $categoryId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to filter by date range.
|
|
|
|
|
*/
|
|
|
|
|
public function scopeDateRange(Builder $query, ?string $range): Builder
|
|
|
|
|
{
|
|
|
|
|
if (empty($range)) {
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return match($range) {
|
|
|
|
|
'today' => $query->whereDate('created_at', today()),
|
|
|
|
|
'week' => $query->where('created_at', '>=', now()->subWeek()),
|
|
|
|
|
'month' => $query->where('created_at', '>=', now()->subMonth()),
|
|
|
|
|
'year' => $query->where('created_at', '>=', now()->subYear()),
|
|
|
|
|
default => $query,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to sort products by popularity (upvotes count).
|
|
|
|
|
*/
|
|
|
|
|
public function scopePopular(Builder $query): Builder
|
|
|
|
|
{
|
|
|
|
|
return $query->withCount('upvotes')->orderBy('upvotes_count', 'desc');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scope a query to sort products.
|
|
|
|
|
*/
|
|
|
|
|
public function scopeSortBy(Builder $query, ?string $sort): Builder
|
|
|
|
|
{
|
|
|
|
|
if (empty($sort)) {
|
|
|
|
|
return $query->latest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return match($sort) {
|
|
|
|
|
'popular' => $query->withCount('upvotes')->orderBy('upvotes_count', 'desc'),
|
|
|
|
|
'oldest' => $query->oldest(),
|
|
|
|
|
'newest' => $query->latest(),
|
|
|
|
|
default => $query->latest(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 14:56:56 +02:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
}
|