- Laravel 11 + Breeze (dark mode) + Tailwind + Alpine.js - Migrations: games, genres, platforms, companies, pivots, user_games, user_lists, reviews, follows, activities - Models: Game, Genre, Platform, Company, UserGame, UserList, Review, ReviewVote, Activity, User (with gamelog fields) - IgdbService: OAuth2 token management, search, game fetch, popular/new releases, genre filter, cover/screenshot URL helpers - IGDB credentials in config/services.php (from .env)
77 lines
2.0 KiB
PHP
77 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Spatie\Sluggable\HasSlug;
|
|
use Spatie\Sluggable\SlugOptions;
|
|
|
|
class Game extends Model
|
|
{
|
|
use HasSlug;
|
|
|
|
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();
|
|
}
|
|
|
|
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,
|
|
]);
|
|
}
|
|
}
|