【发布时间】:2021-10-17 19:44:40
【问题描述】:
我在两个实体/表之间有一对多的关系。
/**
* Get all of the products.
*/
public function products()
{
return $this->belongsToMany(Product::class)->select(
[
'products.id',
'products.title',
'products.sku',
'automation_products.automation_id as auto_id',
'display_order',
]
)->orderBy('display_order');
}
当我想急切加载这种关系时,似乎有重复的查询在后台运行。我使用此代码急切加载我的关系:
$automation = \App\Models\Automation::with('products')->whereId(1)->get()->first();
dump($automation->products()->get());
dump($automation->products()->get());
dump($automation->products()->get());
我有什么遗漏吗?
感谢您的回复。
【问题讨论】:
-
你认为他们为什么会跑?
-
他们正在运行,非常好,请不要与变量名混淆,问题主要围绕一次又一次执行的查询
标签: php laravel eager-loading has-and-belongs-to-many laravel-relations