0
我有一个多态关系,其中一个表'map_nodes'具有一对多到'system_constants'。laravel 5.4多态关系 - 通过查询范围渴望读取
我似乎无法使用查询范围来急切加载system_constants。没有错误,只是没有记录。
这是我的模型。我只包括一个模型“map_nodes”:
namespace App\Modules\Olab\Models;
class SystemConstants extends BaseModel {
// Database:
// id INT(11) PK
// imageable_id INT(11)
// imageable_type VARCHAR(45)
// value BLOB
public function imageable() {
return $this->morphTo();
}
}
class MapNodes extends BaseModel {
public function scopeConstants($query) {
$query->with([ 'SystemConstants' => function ($query) {
$query->select('system_constants.id',
'system_constants.value');
} ]);
}
public function SystemConstants() {
return $this->morphMany('App\Modules\Olab\Models\SystemConstants', 'imageable');
}
}
这是我在system_constants表:
id, imageable_id, imageable_type, value
'1904', '25786', 'Nodes', <blob>
请注意,我有一个关系:: morphMap定义,这让我使用imageable_type中的'节点'。
这是我运行的是什么:
// returns collection of system_constant records
$temp = MapNodes::find(25786)->SystemConstants;
// doesn't work. get map_nodes record, but no eager load of system_constants
$temp = MapNodes::find(25786)->Constants()->first();
我有SQL日志功能,我看到了上述两种查询正确的SQL。我可以在一个SQL工具,准确的查询(用PARAM替换)和它的作品 - 它返回记录:
select `system_constants`.`id`, `system_constants`.`value` 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";}
赞赏任何帮助。
试试这个:'MapNodes :: find(25786) - > constants() - > first();'//常量用小写'c' – musicvicious
感谢您的回复。不幸的是,没有变化。 – cardinalPilot