'date', 'rating_avg' => 'decimal:2', 'similar_game_igdb_ids' => 'array', ]; public function getSlugOptions(): SlugOptions { return SlugOptions::create()->generateSlugsFrom('title')->saveSlugsTo('slug'); } public function getRouteKeyName(): string { return 'slug'; } public function collection(): BelongsTo { return $this->belongsTo(Collection::class); } 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(); } public function comments(): HasMany { return $this->hasMany(GameComment::class); } public function gameModes(): BelongsToMany { return $this->belongsToMany(GameMode::class, 'game_game_mode'); } public function themes(): BelongsToMany { return $this->belongsToMany(Theme::class, 'game_theme'); } public function playerPerspectives(): BelongsToMany { return $this->belongsToMany(PlayerPerspective::class, 'game_player_perspective'); } public function videos(): HasMany { return $this->hasMany(GameVideo::class); } public function websites(): HasMany { return $this->hasMany(GameWebsite::class); } public function similarGames(): BelongsToMany { return $this->belongsToMany( Game::class, 'similar_games', 'game_id', 'similar_game_id' ); } public function toSearchableArray(): array { return [ 'id' => $this->id, 'title' => $this->title, 'summary' => $this->summary, ]; } 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, ]); Cache::forget('home.top_rated'); Cache::forget('games.top_rated.1'); } }