我在laravel中对hasManyThrough的概念感到困惑。 我有三个表:
Bookings
-id (int)
-some other fields
Meta
-id (int)
-booking_id (int)
-metatype_id (int)
-some other fields
MetaType
-id (int)
-name (string)
-some other fields
我想要的是一种Eloquent模型,它能够让我拥有一个包含多个MetaType记录的单个预订记录。我曾尝试使用hasManyThrough来解决这个问题,但现在我认为这可能不是最好的方法。
在我的预订模型中,我有:
public function bookingmeta() {
return $this->hasMany('bookingmeta','booking_id');
}
public function bookingmetatype() {
return $this->hasManyThrough('bookingmetatype','bookingmeta','booking_id','bookingmetatype_id');
}
但是这种方法无法生成正确的SQL,失败了。我得到了:
select `new_bookingmetatype`.*, `new_bookingmeta`.`booking_id`
from `new_bookingmetatype`
inner join `new_bookingmeta`
on `new_bookingmeta`.`bookingmetatype_id` = `new_bookingmetatype`.`id`
where `new_bookingmeta`.`booking_id` in (57103)
而我真正想要实现的是
select `new_bookingmetatype`.*, `new_bookingmeta`.`booking_id`
from `new_bookingmetatype`
inner join `new_bookingmeta`
on `new_bookingmeta`.`id` = `new_bookingmetatype`.`bookingmetatype_id`
where `new_bookingmeta`.`booking_id` in (57103)
如果有人能指点我正确方向,我会非常感激。谢谢。
如果有人能指点我正确方向,我会非常感激。谢谢。