【问题标题】:Laravel 5 hasManyThrough repeating row contentLaravel 5 hasManyThrough 重复行内容
【发布时间】:2015-09-12 16:57:55
【问题描述】:

我有以下数据库表:

tour
   idtour
    other_columns

day
   idday
   other_columns

tour_has_day
   idtour
   idday

每个旅游都有很多天,这些天可以在其他旅游中使用

所以,我在 Tour.php 模型中添加了这个函数:

function days () {
    return $this->hasManyThrough('App\Day', 'App\Tour_has_day', 'idtour', 'idday');
}

它给了我正确的相关天数,但所有行都有相同的内容

我调试它返回的查询:

select 
    `day`.*, 
    `tour_has_day`.`idtour` 
from `day` 
inner join 
    `tour_has_day` on `tour_has_day`.`idtour` = `day`.`idday`
where 
    `tour_has_day`.`idtour` = '1'

然后它返回

【问题讨论】:

  • 这看起来像一个数据透视表,多对多关系对它有用,而不是多对多关系。
  • @JoelHinz 将其发布为答案,我检查它是否成功。谢谢
  • 很高兴,谢谢。我很高兴它有所帮助。

标签: laravel orm model eloquent has-many-through


【解决方案1】:

您需要一个与数据透视表类似的多对多关系。 has-many-through 关系适用于当您有父母和孩子,而孩子是另一个孩子的父母时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-11
    • 2015-05-29
    • 2015-09-10
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    相关资源
    最近更新 更多