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

Posted 8 months ago by malhayek

I have the following tables


TableName : reservation

ModelName: App\Models\Reservation

Columns, id, and few irreverent columns



TableName : reservation_to_users

ModelName: App\Models\ReservationToUser

Columns, id, user_id, reservation_id



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.

Reply to

Use Markdown with GitHub-flavored code blocks.