reconstruction of the project
This commit is contained in:
@@ -3,15 +3,15 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ParentAttendanceReport extends BaseModel
|
||||
{
|
||||
protected $table = 'parent_attendance_reports';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
|
||||
// ✅ CI: useTimestamps = true
|
||||
public $timestamps = true;
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
|
||||
protected $fillable = [
|
||||
'parent_id',
|
||||
'student_id',
|
||||
@@ -28,42 +28,63 @@ class ParentAttendanceReport extends BaseModel
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
protected $validationRules = [
|
||||
'parent_id' => 'required|is_natural_no_zero',
|
||||
'student_id' => 'required|is_natural_no_zero',
|
||||
'report_date' => 'required|valid_date[Y-m-d]',
|
||||
'type' => 'required|in_list[absent,late,early_dismissal]',
|
||||
'arrival_time' => 'permit_empty|regex_match[/^\d{2}:\d{2}(:\d{2})?$/]',
|
||||
'dismiss_time' => 'permit_empty|regex_match[/^\d{2}:\d{2}(:\d{2})?$/]',
|
||||
'semester' => 'permit_empty|string',
|
||||
'school_year' => 'permit_empty|string',
|
||||
'status' => 'permit_empty|in_list[new,seen,processed]',
|
||||
protected $casts = [
|
||||
'parent_id' => 'integer',
|
||||
'student_id' => 'integer',
|
||||
'class_section_id'=> 'integer',
|
||||
'report_date' => 'date',
|
||||
];
|
||||
|
||||
public function listForDateRange(?string $startDate = null, ?string $endDate = null, ?string $schoolYear = null, ?string $semester = null): array
|
||||
/* Optional relationships */
|
||||
public function parent()
|
||||
{
|
||||
$b = $this->builder();
|
||||
if ($startDate) {
|
||||
$b->where('report_date >=', $startDate);
|
||||
return $this->belongsTo(User::class, 'parent_id');
|
||||
}
|
||||
|
||||
public function student()
|
||||
{
|
||||
return $this->belongsTo(Student::class, 'student_id');
|
||||
}
|
||||
|
||||
public function classSection()
|
||||
{
|
||||
// classSection table uses class_section_id as “business key”; your join uses that.
|
||||
return $this->belongsTo(ClassSection::class, 'class_section_id', 'class_section_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Laravel equivalent of CI listForDateRange()
|
||||
* Returns array rows with:
|
||||
* parent_attendance_reports.* + students firstname/lastname + classSection class_section_name
|
||||
*/
|
||||
public static function listForDateRange(
|
||||
?string $startDate = null,
|
||||
?string $endDate = null,
|
||||
?string $schoolYear = null,
|
||||
?string $semester = null
|
||||
): array {
|
||||
$q = DB::table('parent_attendance_reports as par');
|
||||
|
||||
if (!empty($startDate)) {
|
||||
$q->where('par.report_date', '>=', $startDate);
|
||||
}
|
||||
if ($endDate) {
|
||||
$b->where('report_date <=', $endDate);
|
||||
if (!empty($endDate)) {
|
||||
$q->where('par.report_date', '<=', $endDate);
|
||||
}
|
||||
if (is_string($schoolYear) && $schoolYear !== '') {
|
||||
$b->where('school_year', $schoolYear);
|
||||
$q->where('par.school_year', $schoolYear);
|
||||
}
|
||||
if (is_string($semester) && $semester !== '') {
|
||||
$b->where('semester', $semester);
|
||||
$q->where('par.semester', $semester);
|
||||
}
|
||||
|
||||
// Join student + class section labels
|
||||
$b->select('parent_attendance_reports.*, s.firstname, s.lastname, cs.class_section_name')
|
||||
->join('students s', 's.id = parent_attendance_reports.student_id', 'left')
|
||||
->join('classSection cs', 'cs.class_section_id = parent_attendance_reports.class_section_id', 'left')
|
||||
->orderBy('report_date', 'ASC')
|
||||
->orderBy('class_section_id', 'ASC')
|
||||
->orderBy('student_id', 'ASC');
|
||||
$q->leftJoin('students as s', 's.id', '=', 'par.student_id')
|
||||
->leftJoin('classSection as cs', 'cs.class_section_id', '=', 'par.class_section_id')
|
||||
->select('par.*', 's.firstname', 's.lastname', 'cs.class_section_name')
|
||||
->orderBy('par.report_date', 'asc')
|
||||
->orderBy('par.class_section_id', 'asc')
|
||||
->orderBy('par.student_id', 'asc');
|
||||
|
||||
return $b->get()->getResultArray();
|
||||
return $q->get()->map(fn ($r) => (array) $r)->all();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user