简体   繁体   English

Laravel 5.1-渴望将关系加载到同一个表中会执行多个查询

[英]Laravel 5.1 - eager loading relationships to the same table executes multiple queries

I've got a model that has 4 relationships to the same table. 我有一个模型,它与同一张桌子有4个关系。

public function driver()
{
    return $this->belongsTo( Admin::class );
}

public function rejectedBy()
{
    return $this->belongsTo( Admin::class, 'rejected_by' );
}

public function reconciledBy()
{
    return $this->belongsTo( Admin::class, 'reconciled_by' );
}

public function updatedBy()
{
    return $this->belongsTo( Admin::class, 'updated_by' );
}

When I eager load all 4 relationships, the Laravel debugbar reports 4 queries to the admin table. 当我渴望加载所有4个关系时,Laravel调试栏会向admin表报告4个查询。

The method I use is: 我使用的方法是:

$report = Report::find(1)
           ->with('driver', 'rejectedBy', 'reconciledBy', 'updatedBy')
           ->get();

Is there a way to group them together into only one query? 有没有一种方法可以将它们组合为一个查询?

尝试with方法:

$adm = App\Admin::with('driver', 'rejectedBy', 'reconciledBy', 'updatedBy')->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM