Browse Source

feat: busca por contrato

ebagabee 1 tháng trước cách đây
mục cha
commit
9a8143294d

+ 4 - 0
app/Http/Resources/StudentContractResource.php

@@ -40,6 +40,10 @@ public function toArray(Request $request): array
             'payment_method'          => $this->payment_method,
             'fine_cancelled'          => $this->fine_cancelled,
             'status'                  => $this->status,
+            'student_name'            => $this->whenLoaded('student', fn () => $this->student->name),
+            'student_phone'           => $this->whenLoaded('student', fn () => $this->student->phone),
+            'student_city'            => $this->whenLoaded('student', fn () => $this->student->city?->name),
+            'student_state_code'      => $this->whenLoaded('student', fn () => $this->student->state?->code),
             'file_url'                => $this->file_url ? Storage::url($this->file_url) : null,
             'file_type'               => $this->file_type,
             'created_at'              => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),

+ 10 - 0
app/Models/Student.php

@@ -82,4 +82,14 @@ public function unit(): BelongsTo
     {
         return $this->belongsTo(Unit::class, 'unit_id');
     }
+
+    public function city(): BelongsTo
+    {
+        return $this->belongsTo(City::class, 'city_id');
+    }
+
+    public function state(): BelongsTo
+    {
+        return $this->belongsTo(State::class, 'state_id');
+    }
 }

+ 2 - 1
app/Services/StudentContractService.php

@@ -11,7 +11,8 @@ class StudentContractService
 {
     public function getAll(int $unitId, ?int $studentId = null): Collection
     {
-        return StudentContract::where('unit_id', $unitId)
+        return StudentContract::with(['student.city', 'student.state'])
+            ->where('unit_id', $unitId)
             ->when($studentId, fn ($q) => $q->where('student_id', $studentId))
             ->orderBy('created_at', 'desc')
             ->get();