2026-05-29 00:33:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2026-05-30 11:23:24 +03:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
use Laravel\Scout\Searchable;
|
2026-05-29 00:33:00 +03:00
|
|
|
use Spatie\Sluggable\HasSlug;
|
|
|
|
|
use Spatie\Sluggable\SlugOptions;
|
|
|
|
|
|
|
|
|
|
class Game extends Model
|
|
|
|
|
{
|
2026-05-30 11:23:24 +03:00
|
|
|
use HasSlug, Searchable;
|
2026-05-29 00:33:00 +03:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'igdb_id', 'slug', 'title', 'summary', 'cover_url', 'cover_thumb_url',
|
|
|
|
|
'release_date', 'release_year', 'rating_avg', 'rating_count', 'review_count',
|
|
|
|
|
'library_count', 'status',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'release_date' => 'date',
|
|
|
|
|
'rating_avg' => 'decimal:2',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function getSlugOptions(): SlugOptions
|
|
|
|
|
{
|
|
|
|
|
return SlugOptions::create()->generateSlugsFrom('title')->saveSlugsTo('slug');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getRouteKeyName(): string { return 'slug'; }
|
|
|
|
|
|
|
|
|
|
public function genres(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Genre::class, 'game_genre');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function platforms(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Platform::class, 'game_platform');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function companies(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Company::class, 'game_company')->withPivot('role');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function screenshots(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(GameScreenshot::class)->orderBy('order');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function userGames(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(UserGame::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function reviews(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Review::class)->latest();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 11:23:24 +03:00
|
|
|
public function toSearchableArray(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'id' => $this->id,
|
|
|
|
|
'title' => $this->title,
|
|
|
|
|
'summary' => $this->summary,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 00:33:00 +03:00
|
|
|
public function scopePublished($query)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('status', 'published');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function recalculateRating(): void
|
|
|
|
|
{
|
|
|
|
|
$stats = $this->userGames()->whereNotNull('rating')->selectRaw('AVG(rating) as avg, COUNT(*) as cnt')->first();
|
|
|
|
|
$this->update([
|
|
|
|
|
'rating_avg' => $stats->avg ? round($stats->avg, 2) : null,
|
|
|
|
|
'rating_count' => $stats->cnt ?? 0,
|
|
|
|
|
]);
|
2026-05-30 11:23:24 +03:00
|
|
|
|
|
|
|
|
Cache::forget('home.top_rated');
|
|
|
|
|
Cache::forget('games.top_rated.1');
|
2026-05-29 00:33:00 +03:00
|
|
|
}
|
|
|
|
|
}
|