Laravel Relationship HasManyThrough

时间:2018-05-08 15:03:41

标签: laravel eloquent

我在使用Laravel / Eloquents关系语法在我的应用程序中建立关系时遇到了麻烦。

我有:

Events     - 身份证,姓名,地点

Reservations     - id,event_id,user_id

Users     - 身份证,姓名等

我希望能够抓住“给定事件的所有预订中的所有用户对象”。我已尝试将hasManyThrough与各种参数集一起使用以获得此结果集,但我想出了空集!

感谢任何见解。

1 个答案:

答案 0 :(得分:1)

如果您正在寻找users关系,reservation_id表应具有预留表的hasManyThrough外键。

如果您需要获取

  

所有用户对象都是针对给定事件的所有预订

您可以使用

$users_with_reservation = Reservation::where('event_id', $event_id)->pluck('user_id');

$users = User::whereIn('id', $users_with_reservation)->get();