【问题标题】:Get the "many" entries of a relationship获取关系的“许多”条目
【发布时间】:2017-05-10 12:05:15
【问题描述】:

我在 eloquent 中声明了一对多的关系。

  • 一集多集,一集多集。

我了解如何检索剧集的 serial 数据:

TheEpisode::where('id', '=', $id)->take(1)->with('TheParts', 'TheAuthor')

但是我如何做同样的事情,检索电视剧的剧集?

`Table: TheSeries`
id      | SerialName 
---------------------
1       | Zootopia
2       | Moana
3       | Toy Story

`Table: TheEpisodes`
seriesID| episode
---------------------
1       | 10
2       | 10
3       | 10
1       | 11
2       | 11
2       | 12

我需要 Moana 电视连续剧的所有剧集(应该是剧集:10、11、12)

应该如何查询?

【问题讨论】:

    标签: php laravel serialization


    【解决方案1】:

    使用eager loading 获取所有剧集的连续剧:

    Serial::where('id', $serialId)->with('episodes')->first();
    

    获取所有剧集:

    TheEpisode::where('serial_id', $serialId)->get();
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 2019-07-17
      相关资源
      最近更新 更多