dsfasdfsda234234 2016-08-14 08:19
浏览 13
已采纳

Laravel加载相关的类模型

I need a comprehensive explanation on this excerpt from Martin Bean Laravel Essential :

  $breed = Furbook\Breed::with('cats') 
- >whereName($name) 
- >first() 

I understand all the method except with(). Pls explain in sql what the with('cat') is used for. Pls my code might not be formatted well because I used the app.

  • 写回答

2条回答 默认 最新

  • dqs66973 2016-08-14 12:17
    关注

    If you take a look at in documentation, you will see 'with' is for eager loading. It could be explained as follows.

    select * from books where id = 1
    
    select * from books where id = 2
    
    select * from books where id = 3
    
    select * from books where id = 4
    ....
    

    This query is reduce to

    select * from books
    
    select * from authors where id in (1, 2, 3, 4, 5, ...)
    

    which is more efficient.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部