How can I get users through a relation table using Eloquent?

Posted 3 months ago by malhayek

I have the following tables

Reservations

TableName : reservation

ModelName: App\Models\Reservation

Columns, id, and few irreverent columns

=================================

Relations

TableName : reservation_to_users

ModelName: App\Models\ReservationToUser

Columns, id, user_id, reservation_id

=================================

Users

TableName : users

ModelName: App\Models\User

Columns, id, and few irreverent columns

=================================

On the App\Models\Reservation model, I want to be access all users that are associated with the current reservation.

What relation type do I use MorphToMany or hasManyThrough?

I tried using

hasManyThrough('App\Models\User', 'users')

but that is throwing SQL errors

What is the right way to access the users from the reservation?

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.