1
我正在运行Laravel 5.4,出于某种原因,我不能做1对多态关系的列选择。我想限制在相关表中返回的列。laravel雄辩 - 不能急于加载多态关系与查询'选择'
这里是我的1对多的关系,我的“1个侧”:
class MapNodes extends Model {
protected $table = 'map_nodes';
protected $fillable = ['title'];
protected $validations = ['title' => 'max:200|string' ];
public function SystemConstants() {
return $this->morphMany('App\Modules\Models\SystemConstants', 'imageable');
}
}
下面是关系我的“很多副作用”表:
class SystemConstants extend Model {
protected $table = 'system_constants';
protected $fillable = ['name','imageable_id','imageable_type'];
protected $validations = ['name' => 'max:200|string',
'imageable_id' => 'integer|required',
'imageable_type' => 'max:45|string'];
// define this model as polymorphic
public function imageable() {
return $this->morphTo();
}
}
这里有两种方法,我试图打电话给它。一沾到SystemConstants所有列,第二个我只想两列:
$temp = MapNodes::with('SystemConstants')->find(25786);
$temp = MapNodes::with([ 'SystemConstants' =>
function($query) {
return $query->select('system_constants.id', 'system_constants.name');
} ])->find(25786);
为什么第一次调用返回的相关记录,但不是第二个?这两个调用的下面的SQL语句看起来完全一样(除了第二次调用时只需要两列)。
select * from `system_constants` where
`system_constants`.`imageable_id` in (?) and
`system_constants`.`imageable_type` = ? - a:2:{i:0;i:25786;i:1;s:5:"Nodes";}
select `system_constants`.`id`, `system_constants`.`name` from `system_constants` where
`system_constants`.`imageable_id` in (?) and
`system_constants`.`imageable_type` = ? - a:2:{i:0;i:25786;i:1;s:5:"Nodes";}
谢谢!我不这样做,因为我的两个语句之间生成的SQL是相同的,但它的工作原理! – cardinalPilot