61 lines
1.5 KiB
PHP
61 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
|
|
|
class Room extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $guarded = [];
|
|
|
|
public function auditions(): HasMany
|
|
{
|
|
return $this->hasMany(Audition::class)->orderBy('order_in_room')->orderBy('score_order');
|
|
}
|
|
|
|
public function prelimAuditions(): HasMany
|
|
{
|
|
return $this->hasMany(PrelimDefinition::class);
|
|
}
|
|
|
|
public function entries(): HasManyThrough
|
|
{
|
|
return $this->hasManyThrough(
|
|
Entry::class,
|
|
Audition::class,
|
|
'room_id', // Foreign key on the audition table
|
|
'audition_id', //Foreign key on the Entries table
|
|
'id', // Local key on the room table
|
|
'id' // Local key on the Auditions table
|
|
);
|
|
}
|
|
|
|
public function users(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class, 'room_user');
|
|
}
|
|
|
|
public function judges(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class, 'room_user', 'room_id', 'user_id');
|
|
}
|
|
|
|
public function addJudge($userId): void
|
|
{
|
|
$this->judges()->attach($userId);
|
|
$this->load('judges');
|
|
}
|
|
|
|
public function removeJudge($userId): void
|
|
{
|
|
$this->judges()->detach($userId);
|
|
$this->load('judges');
|
|
}
|
|
}
|