Laravel eagerload自定义查询而不是关系

时间:2015-12-26 19:39:39

标签: php laravel laravel-5

我有一个查询,我想加载,但它无法正常工作。

我以前渴望加载关系并且它工作正常但是这一直给我这个错误:

Call to a member function addEagerConstraints() on null

我在模型中的功能:

public function update_image()
{
    return UpdateItem::where('type', 'image')->where('update_id', $this->id)->first();
}

它叫:

$updates = Update::with('project', 'update_items', 'update_image')->get();

我知道查询本身很好,但是哪里出错了?

1 个答案:

答案 0 :(得分:1)

据我所知,除了关系之外你不能急于加载任何东西,但你可以将你的查询添加到关系中:在这种情况下(我假设它是基于你的问题的一对一)。

public function update_image()
{
     return $this->hasOne('App\UpdateItem','update_id','id')->where('type', 'image');
}