【发布时间】:2017-05-23 19:41:55
【问题描述】:
我怎样才能从多态关系中只加载特定的列?
我有一个 people 表,它可以变形为用户和机器人,我只需要从其中加载 first_name 和 last_name 以进行特定查询。
我试过this solution 没有运气,因为它似乎不适用于多态关系......它只是忽略了->select() 并按应有的方式加载所有内容。
有什么想法吗?
编辑: 这是相关查询
$school->load(array(
'associationsTeachers' => function($q){
$q->with(array(
'person' => function($q2){
$q2->with(array(
'account' => function($q3){
$q3->select(array('id', 'first_name', 'last_name', 'person_id', 'account_id', 'account_type' ));
}
));
},
));
}
));
这里是关系
人
table: 'people';
public function account(){
return $this->morphTo();
}
用户
table: 'users'
public function person(){
return $this->morphOne('Person', 'account');
}
机器人
table: 'bots'
public function person(){
return $this->morphOne('Person', 'account');
}
【问题讨论】:
-
我也遇到了同样的问题。但我找到了github.com/laravel/framework/issues/2966。 Laravel 的作者说不支持限制对这些关系的选择
标签: laravel laravel-4 eloquent