quiniela-sembradores-backend/app/Models/Pick.php

55 lines
1.1 KiB
PHP

<?php
namespace App\Models;
use App\Enums\MatchOutcome;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Fillable([
'user_id',
'match_id',
'selection',
'points_awarded',
'submitted_at',
'graded_at',
])]
class Pick extends Model
{
/**
* @return array<string, string>
*/
protected $fillable = [
'user_id',
'match_id',
'selection',
'submitted_at',
];
protected function casts(): array
{
return [
'selection' => MatchOutcome::class,
'points_awarded' => 'integer',
'submitted_at' => 'datetime',
'graded_at' => 'datetime',
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function fixture(): BelongsTo
{
return $this->belongsTo(Fixture::class, 'match_id');
}
public function isCorrect(): bool
{
return $this->fixture?->result !== null
&& $this->selection === $this->fixture->result;
}
}