dimanche 31 mars 2019

Laravel whereHas getting the relationship

I'm doing a Laravel application to manage events. An Attendant and and Event have a belongsToMany relationship. An attendant has multiple events and viceversa. The pivot column has an extra field called uuid, this is like the ticket id to the event. I need to validate the entrance of an Attendant to an Event. So the Attendant scans a QR code with the uuid of the pivot table.

So I have this function

public function validateTicket(Request $request, $ticket) {
    $event = Event::fromTicket($ticket);
    \Log::info($event);
    return new EventResource($event);
}

And the static method fromTicket is:

public static function fromTicket($ticket) {
    return static::whereHas('attendants', function($q) use ($ticket) {
        $q->where('uuid', $ticket);
    })->first();
}

The problem is that I need the relationship too, this because I need to register the hour of entrance of an Attendant, and this entrance could happen many times.

Is there a way to return the relationship too during the whereHas query?

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire