我正在尝试在 laravel 中急切加载模型,但只返回某些列。我不希望呈现整个急切加载的表格。
public function car()
{
return $this->hasOne('Car', 'id')->get(['emailid','name']);
}
我收到以下错误:
log.ERROR: 异常 'Symfony\Component\Debug\Exception\FatalErrorException' 带有消息 'Call to undefined method Illuminate\Database\Eloquent\Collection::getAndResetWheres()'
最佳答案
利用 select()
方法:
public function car() {
return $this->hasOne('Car', 'id')->select(['owner_id', 'emailid', 'name']);
}
注意:记得添加分配给匹配两个表的外键的列。例如,在我的示例中,我假设 Owner
有一个 Car
,这意味着分配给外键的列类似于 owners.id = cars .owner_id
,所以我必须将 owner_id
添加到所选列的列表中;
关于php - Laravel Eager Loading - 只加载特定的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16994253/