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

codestorm's avatar

hasManyThrough

here my tables:

    bookings
    --------------------
    id
    user_id
    course_id
    course_dates_id
    season_id
    start
    stop
    ...
    course_dates
    --------------------
    id
    course_id
    weekday
    ...
    users
    ---------------------
    id
    password
    firstname
    lastname
    ...
    courses
    ------------------
    id
    name
    teacher_id
    ...

I want to go through the dates to show which user has booked the course to the specific date like so

course ->
     date 1 -> 
          users (has booked the course for this date)
     date 2 ->
          users (has booked the course for this date)
     ...

I tried every combination, this is the right query:

select `koup_users`.*, `koup_bookings`.`course_dates_id` from `koup_users` inner join `koup_bookings` on `koup_bookings`.`user_id` = `koup_users`.`id` where `koup_bookings`.`course_dates_id` in (10, 11)

but with my relationship (model Dates):

    public function users()
        {
            return $this->hasManyThrough('User', 'Booking', 'course_dates_id', 'id');
        }

i get this query:

    select `koup_users`.*, `koup_bookings`.`course_dates_id` from `koup_users` inner join `koup_bookings` on `koup_bookings`.`id` = `koup_users`.`id` where `koup_bookings`.`course_dates_id` in (10, 11)

But koup_bookings.id has to be koup_bookings.user_id!!!

Can anyone help me? Or is it a bad database structure? Thanks.

0 likes
1 reply

Please or to participate in this conversation.