reconstruction of the project
This commit is contained in:
@@ -2,61 +2,116 @@
|
||||
|
||||
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';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* CI: timestamps disabled
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'student_id',
|
||||
'condition_name',
|
||||
];
|
||||
public $timestamps = false;
|
||||
|
||||
protected $validationRules = [
|
||||
'student_id' => 'required|is_natural_no_zero',
|
||||
'condition_name' => 'required|string|max_length[100]',
|
||||
protected $casts = [
|
||||
'student_id' => 'integer',
|
||||
];
|
||||
|
||||
protected $validationMessages = [
|
||||
'student_id' => [
|
||||
'required' => 'Student ID is required.',
|
||||
'is_natural_no_zero' => 'Invalid Student ID.',
|
||||
],
|
||||
'condition_name' => [
|
||||
'required' => 'Condition name is required.',
|
||||
'max_length' => 'Condition name must be 100 characters or fewer.',
|
||||
],
|
||||
];
|
||||
/* ============================================================
|
||||
* 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);
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* CI-compatible methods
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get all medical conditions for a given student.
|
||||
*
|
||||
* @param int $studentId
|
||||
* @return array
|
||||
* CI: getMedicalByStudentId()
|
||||
*/
|
||||
public function getMedicalByStudentId(int $studentId): array
|
||||
public static function getMedicalByStudentId(int $studentId): array
|
||||
{
|
||||
return $this->where('student_id', $studentId)
|
||||
->findAll();
|
||||
}
|
||||
return static::query()
|
||||
->forStudent($studentId)
|
||||
->orderBy('id', 'asc')
|
||||
->get()
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get medical conditions for multiple students.
|
||||
*
|
||||
* @param array $studentIds
|
||||
* @return array grouped by student_id
|
||||
/**
|
||||
* Get medical conditions for multiple students, grouped by student_id.
|
||||
* CI: getMedicalByStudentIds()
|
||||
*/
|
||||
public function getMedicalByStudentIds(array $studentIds): array
|
||||
public static function getMedicalByStudentIds(array $studentIds): array
|
||||
{
|
||||
$rows = $this->whereIn('student_id', $studentIds)->findAll();
|
||||
$rows = static::query()
|
||||
->forStudents($studentIds)
|
||||
->orderBy('student_id', 'asc')
|
||||
->orderBy('id', 'asc')
|
||||
->get();
|
||||
|
||||
$grouped = [];
|
||||
foreach ($rows as $row) {
|
||||
$grouped[$row['student_id']][] = $row;
|
||||
$sid = (int) $row->student_id;
|
||||
$grouped[$sid][] = $row;
|
||||
}
|
||||
|
||||
return $grouped;
|
||||
}
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Validation helper (FormRequest/controller)
|
||||
* Mirrors CI 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.',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user