54 lines
1.2 KiB
PHP
54 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Str;
|
|
|
|
#[Fillable([
|
|
'name',
|
|
'short_name',
|
|
'slug',
|
|
'fifa_code',
|
|
'flag_url',
|
|
'is_active',
|
|
])]
|
|
class Team extends Model
|
|
{
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $team): void {
|
|
if (blank($team->slug) && filled($team->name)) {
|
|
$team->slug = Str::slug($team->name);
|
|
}
|
|
});
|
|
|
|
static::updating(function (self $team): void {
|
|
if (blank($team->slug) && filled($team->name)) {
|
|
$team->slug = Str::slug($team->name);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'is_active' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function homeFixtures(): HasMany
|
|
{
|
|
return $this->hasMany(Fixture::class, 'home_team_id');
|
|
}
|
|
|
|
public function awayFixtures(): HasMany
|
|
{
|
|
return $this->hasMany(Fixture::class, 'away_team_id');
|
|
}
|
|
}
|