2016-12-25 76 views
1

我在宣称的雄辩中拥有一对多的关系。获取关系中的“许多”条目

  • 一个系列有更多的情节,一个情节有一个系列。

我明白了如何检索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 

我需要莫阿纳电视连续剧的所有专题节目(这应该是情节:10,11,12)

我应该如何查询呢?

回答

2

使用eager loading获取序列与所有它的情节:

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

获取所有情节:

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