auditionadmin/app/Models/Student.php

107 lines
2.6 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;
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();
}
}