2026-05-29 00:33:00 +03:00
|
|
|
<?php
|
2026-05-30 02:50:34 +03:00
|
|
|
|
2026-05-29 00:33:00 +03:00
|
|
|
namespace App\Models;
|
2026-05-30 02:50:34 +03:00
|
|
|
|
2026-05-30 11:23:24 +03:00
|
|
|
use App\Services\ActivityService;
|
2026-05-29 00:33:00 +03:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
|
|
|
|
|
class Review extends Model
|
|
|
|
|
{
|
2026-05-30 02:50:34 +03:00
|
|
|
protected $fillable = ['user_id', 'game_id', 'body', 'rating', 'has_spoilers'];
|
|
|
|
|
|
|
|
|
|
protected $casts = ['has_spoilers' => 'boolean'];
|
|
|
|
|
|
|
|
|
|
protected static function boot(): void
|
|
|
|
|
{
|
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
|
|
static::saved(function (Review $review) {
|
|
|
|
|
$review->game->recalculateRating();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
static::deleted(function (Review $review) {
|
|
|
|
|
$review->game->recalculateRating();
|
|
|
|
|
});
|
2026-05-30 11:23:24 +03:00
|
|
|
|
|
|
|
|
static::created(function (Review $review) {
|
|
|
|
|
ActivityService::log($review->user, 'reviewed_game', $review->game, ['rating' => $review->rating]);
|
|
|
|
|
});
|
2026-05-30 02:50:34 +03:00
|
|
|
}
|
2026-05-29 00:33:00 +03:00
|
|
|
|
|
|
|
|
public function user(): BelongsTo { return $this->belongsTo(User::class); }
|
|
|
|
|
public function game(): BelongsTo { return $this->belongsTo(Game::class); }
|
|
|
|
|
public function votes(): HasMany { return $this->hasMany(ReviewVote::class); }
|
|
|
|
|
}
|