%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/camillo/camillo-api-master/app/Http/Resources/
Upload File :
Create Path :
Current File : //var/www/html/camillo/camillo-api-master/app/Http/Resources/EventResource.php

<?php

namespace App\Http\Resources;

use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class EventResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        $date_as_int = (int) Carbon::parse($request->date)->format('Ymd');
        /** @var \App\Models\Minor $this */
        /*$events = $this->events()->where('events.date_as_int', '=', $date_as_int)
            ->join('event_types', 'event_types.id', '=', 'events.event_type_id');*/
        $arrival = $this->events()->where('events.date_as_int', '=', $date_as_int)
                        ->join('event_types', 'event_types.id', '=', 'events.event_type_id')->where(function ($q) {
                $q->where('event_type', 'presence_confirmed');
                $q->orWhere('event_type', 'qr_minor_arrived');
            })->orderByRaw("FIELD(event_types.event_type, 'presence_confirmed', 'qr_minor_arrived')")->first();

        $left = $this->events()->where('events.date_as_int', '=', $date_as_int)
                     ->join('event_types', 'event_types.id', '=', 'events.event_type_id')->where(function ($q) {
                $q->where('event_type', 'qr_minor_left');
                $q->orWhere('event_type', 'minor_left_class');
            })->orderByRaw("FIELD(event_types.event_type, 'qr_minor_left', 'minor_left_class')")->first();

        $absent = $this->events()->where('events.date_as_int', '=', $date_as_int)
                       ->join('event_types', 'event_types.id', '=', 'events.event_type_id')->where(function ($q) {
                $q->where('event_type', 'expected_absence_signaled');
                $q->orWhere('event_type', 'minor_absence_teacher');
            })->first();
        $data = [
            "id"            => $this->id,
            "name"          => $this->name,
            "surname"       => $this->surname,
            "arrival_time"  => $arrival
                ? Carbon::parse($arrival->date)->format('H:i')
                : null,
            "arrival_event" => $arrival
                ? $arrival->eventType->event_type
                : null,
            "left_time"     => $left
                ? Carbon::parse($left->date)->format('H:i')
                : null,
            'left_event'    => $left
                ? $left->eventType->event_type
                : null,
            'absent'        => $absent
                ? true
                : false,
            'absent_event'  => $absent
                ? $absent->eventType->event_type
                : null,
        ];

        return $data;
    }
}

Zerion Mini Shell 1.0