Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Saneesh's avatar

How to get date from the dependent table

Hi

I have models with some relations. User -> EventDate -> EventTimes

class User {
    public function eventDates()
    {
        return $this->hasMany(EventDate::class);
    }

    public function eventTimes()
    {
        return $this->hasMany(EventTime::class);
    }
}

class EventDate {
    public function users() 
    {
        return $this->belongsTo(User::class);
    }
      
    public function eventTimes()
    {
        return $this->hasMany(EventTime::class);
    }
}


class EventTime {
   public function eventDates()
    {
        return $this->belongsTo(EventDate::class);
    }	

}

I have a function to get the events by date of the user.

public function getEventsByDate(array $attributes) {
        return auth()->user()::with([
            'eventDates' => function ($query) use ($attributes) {
                $query->whereDate('event_date', $attributes['event_date']);
            }])
        ->where('id', $attributes['user_id'])
        ->get()
        ->first();
}

Here, from User I can get eventDates but how can I get the eventTimes of eventDates

I can call like this return auth()->user()->load('eventDates.eventTimes');, but if I want filter for and eventDay how can I do it?

0 likes
1 reply

Please or to participate in this conversation.