关于 Laravel 预加载问题请教

现有数据库
1、sale(id,stroe_id,name)
sale Mode 里面关联:

    public function store()
    {
        return $this->belongsTo(Store::class);
    }

2、stroe 字段(id,mdname);

查询代码按照教程写的如下:

        $sales = Sale::limit(3)->get();
        foreach ($sales as $book) {
            echo $book->store->mdname;
        }
    $sales = Sale::limit(3)->with(['Store'])->get();
    foreach ($sales as $book) {
        echo $book->store->mdname;
    }

数据库查询日志:
[2018-10-01 15:19:53] local.INFO: select from sales limit 3
[2018-10-01 15:19:53] local.INFO: select
from stores where stores.id = ? limit 1 ["153"]
[2018-10-01 15:19:53] local.INFO: select from stores where stores.id = ? limit 1 ["148"]
[2018-10-01 15:19:53] local.INFO: select
from stores where stores.id = ? limit 1 ["166"]
[2018-10-01 15:19:53] local.INFO: select from sales limit 3
[2018-10-01 15:19:53] local.INFO: select
from stores where stores.id in (?, ?, ?) ["148","153","166"]
[2018-10-01 15:19:53] local.INFO: select from stores where stores.id = ? limit 1 ["153"]
[2018-10-01 15:19:53] local.INFO: select
from stores where stores.id = ? limit 1 ["148"]
[2018-10-01 15:19:53] local.INFO: select * from stores where stores.id = ? limit 1 ["166"]

并没有实现预加载 减少N+1 问题?求解

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1
leo

$sales = Sale::limit(3)->with(['store'])->get();

没事不要乱改大小写

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!