Laravel Eager Loading - Load only specific columns(Laravel Eager Loading - 仅加载特定列)
问题描述
我正在尝试在 laravel 中急切加载模型,但只返回某些列.我不希望呈现整个急切加载的表格.
I am trying to eager load a model in laravel but only return certain columns. I do not want the whole eager loaded table being presented.
public function car()
{
return $this->hasOne('Car', 'id')->get(['emailid','name']);
}
我收到以下错误:
log.ERROR: 异常 'SymfonyComponentDebugExceptionFatalErrorException' 带有消息 'Call to undefined method IlluminateDatabaseEloquentCollection::getAndResetWheres()'
log.ERROR: exception 'SymfonyComponentDebugExceptionFatalErrorException' with message 'Call to undefined method IlluminateDatabaseEloquentCollection::getAndResetWheres()'
推荐答案
利用select()方法:
public function car() {
return $this->hasOne('Car', 'id')->select(['owner_id', 'emailid', 'name']);
}
注意:记得添加分配给匹配两个表的外键的列.例如,在我的示例中,我假设 Owner 有一个 Car,这意味着分配给外键的列类似于 owners.id = cars.owner_id,所以我必须将 owner_id 添加到所选列的列表中;
Note: Remember to add the columns assigned to the foreign key matching both tables. For instance, in my example, I assumed a Owner has a Car, meaning that the columns assigned to the foreign key would be something like owners.id = cars.owner_id, so I had to add owner_id to the list of selected columns;
这篇关于Laravel Eager Loading - 仅加载特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel Eager Loading - 仅加载特定列
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
