douyan8266
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:

    http://laravel.com/docs/5.1/eloquent-relationships#querying-relations

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题