关于 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 问题?求解
推荐文章: