117 lines
3.3 KiB
PHP
117 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use App\Models\BaseModel;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class StudentMedicalCondition extends BaseModel
|
|
{
|
|
protected $table = 'student_medical_conditions';
|
|
|
|
/**
|
|
* legacy: timestamps disabled
|
|
*/
|
|
public $timestamps = true;
|
|
|
|
protected $fillable = [
|
|
'student_id',
|
|
'condition_name',
|
|
];
|
|
|
|
protected $casts = [
|
|
'student_id' => 'integer',
|
|
];
|
|
|
|
/* ============================================================
|
|
* Relationships (optional)
|
|
* ============================================================
|
|
*/
|
|
|
|
public function student(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Student::class, 'student_id');
|
|
}
|
|
|
|
/* ============================================================
|
|
* Scopes
|
|
* ============================================================
|
|
*/
|
|
|
|
public function scopeForStudent(Builder $q, int $studentId): Builder
|
|
{
|
|
return $q->where('student_id', $studentId);
|
|
}
|
|
|
|
public function scopeForStudents(Builder $q, array $studentIds): Builder
|
|
{
|
|
$ids = array_values(array_unique(array_filter(array_map('intval', $studentIds))));
|
|
return empty($ids) ? $q->whereRaw('1=0') : $q->whereIn('student_id', $ids);
|
|
}
|
|
|
|
/* ============================================================
|
|
* legacy-compatible methods
|
|
* ============================================================
|
|
*/
|
|
|
|
/**
|
|
* Get all medical conditions for a given student.
|
|
* legacy: getMedicalByStudentId()
|
|
*/
|
|
public static function getMedicalByStudentId(int $studentId): array
|
|
{
|
|
return static::query()
|
|
->forStudent($studentId)
|
|
->orderBy('id', 'asc')
|
|
->get()
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* Get medical conditions for multiple students, grouped by student_id.
|
|
* legacy: getMedicalByStudentIds()
|
|
*/
|
|
public static function getMedicalByStudentIds(array $studentIds): array
|
|
{
|
|
$rows = static::query()
|
|
->forStudents($studentIds)
|
|
->orderBy('student_id', 'asc')
|
|
->orderBy('id', 'asc')
|
|
->get();
|
|
|
|
$grouped = [];
|
|
foreach ($rows as $row) {
|
|
$sid = (int) $row->student_id;
|
|
$grouped[$sid][] = $row;
|
|
}
|
|
|
|
return $grouped;
|
|
}
|
|
|
|
/* ============================================================
|
|
* Validation helper (FormRequest/controller)
|
|
* Mirrors legacy rules/messages
|
|
* ============================================================
|
|
*/
|
|
|
|
public static function rules(bool $updating = false): array
|
|
{
|
|
$req = $updating ? 'sometimes' : 'required';
|
|
|
|
return [
|
|
'student_id' => [$req, 'integer', 'min:1', 'exists:students,id'],
|
|
'condition_name' => [$req, 'string', 'max:100'],
|
|
];
|
|
}
|
|
|
|
public static function messages(): array
|
|
{
|
|
return [
|
|
'student_id.required' => 'Student ID is required.',
|
|
'student_id.min' => 'Invalid Student ID.',
|
|
'condition_name.required' => 'Condition name is required.',
|
|
'condition_name.max' => 'Condition name must be 100 characters or fewer.',
|
|
];
|
|
}
|
|
} |