2015-10-30 16:09
浏览 23

Laravel 5.1 - 返回具有多种关系的雄辩模型

I have the following models Show, Presenter, Image.

Shows can have multiple presenters. Presenters have one image.

I can do this to get a presenter with their image:

$presenter = Presenter::with('image)->find(1);

And I can do this to get a show with presenters:

$show = Show::with('presenters')->find(1);

Is there a way I can return a show with presenters and their image in one statement?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doushi1996 2015-10-30 16:22

    You can use the dot notation to eager load nested relationships.

    $show = Show::with('presenters.image')->find(1);
    解决 无用
    打赏 举报
  • dongshi8359 2015-10-30 16:43

    If you defined your relations you can get them all in one query very easy:

    解决 无用
    打赏 举报

相关推荐 更多相似问题