123 lines
3.1 KiB
PHP
123 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class Student extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public static $shirtSizes = [
|
|
'none' => '---',
|
|
'YS' => 'Youth Small',
|
|
'YM' => 'Youth Medium',
|
|
'YL' => 'Youth Large',
|
|
'YXL' => 'Youth Extra Large',
|
|
'S' => 'Small',
|
|
'M' => 'Medium',
|
|
'L' => 'Large',
|
|
'XL' => 'Extra Large',
|
|
'2XL' => '2XL',
|
|
'3XL' => '3XL',
|
|
];
|
|
|
|
protected $guarded = [];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'optional_data' => 'array',
|
|
];
|
|
}
|
|
|
|
public function nominations(): HasMany
|
|
{
|
|
return $this->hasMany(NominationEnsembleEntry::class);
|
|
}
|
|
|
|
public function school(): BelongsTo
|
|
{
|
|
return $this->belongsTo(School::class);
|
|
}
|
|
|
|
public function historicalSeats(): HasMany
|
|
{
|
|
return $this->hasMany(HistoricalSeat::class);
|
|
}
|
|
|
|
/**
|
|
* Returns the directors at this student's school.
|
|
*/
|
|
public function users(): HasManyThrough
|
|
{
|
|
return $this->hasManyThrough(
|
|
User::class, // The target model we want to access
|
|
School::class, // The intermediate model through which we access the target model
|
|
'id', // The foreign key on the intermediate model
|
|
'school_id', // The foreign key on the target model
|
|
'school_id', // The local key
|
|
'id' // The local key on the intermediate model
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Returns the directors at this student's school.
|
|
* Alias of users())
|
|
* '
|
|
*/
|
|
public function directors(): HasManyThrough
|
|
{
|
|
return $this->users();
|
|
}
|
|
|
|
public function entries(): HasMany
|
|
{
|
|
return $this->hasMany(Entry::class);
|
|
}
|
|
|
|
public function full_name(bool $last_name_first = false): string
|
|
{
|
|
if ($last_name_first) {
|
|
return $this->last_name.', '.$this->first_name;
|
|
}
|
|
|
|
return $this->first_name.' '.$this->last_name;
|
|
}
|
|
|
|
public function doublerRequests(): HasMany
|
|
{
|
|
return $this->hasMany(DoublerRequest::class);
|
|
}
|
|
|
|
public function isDoublerInEvent(Event|int $event): bool
|
|
{
|
|
$eventId = $event instanceof Event ? $event->id : $event;
|
|
|
|
return DoublerEntryCount::where([
|
|
'event_id' => $eventId,
|
|
'student_id' => $this->id,
|
|
])->exists();
|
|
}
|
|
|
|
public function entriesForEvent(Event|int $event): Collection
|
|
{
|
|
$eventId = $event instanceof Event ? $event->id : $event;
|
|
|
|
return Entry::query()
|
|
->where('student_id', $this->id)
|
|
->whereHas('audition', function ($query) use ($event) {
|
|
$query->where('event_id', $event);
|
|
})
|
|
->with('audition.SeatingLimits') // Eager load the audition relation if needed
|
|
->with('totalScore')
|
|
->get();
|
|
|
|
}
|
|
}
|