Php 跨多个表的Laravel关系失败
我有以下表格 查表Php 跨多个表的Laravel关系失败,php,laravel,Php,Laravel,我有以下表格 查表 id name ... public function settingsval(){ return $this->hasMany('App\AppSettingTypes','name','name.setting'); } 表格设置类型 id name level //references id on table check public function settings(){ return $this-&
id
name ...
public function settingsval(){
return $this->hasMany('App\AppSettingTypes','name','name.setting');
}
表格设置类型
id
name
level //references id on table check
public function settings(){
return $this->hasMany('App\AppSetting','id','type');
}
选项卡设置
type, //references type on table setting type
name
value
所以基本上我想返回所有带有设置的检查
所以我的模型中有
1.检查模型//参考表检查
id
name ...
public function settingsval(){
return $this->hasMany('App\AppSettingTypes','name','name.setting');
}
在my AppSettingTypes//上引用表设置类型
id
name
level //references id on table check
public function settings(){
return $this->hasMany('App\AppSetting','id','type');
}
所以在我的控制器上,我只是在做
CheckModel::with('settingsval')
但每次设置数组都是空的,即使有数据
有什么问题吗?如果表的
设置类型
的级别
列保存了对检查表的引用,则映射如下
public function settingsval(){
return $this->hasMany('App\AppSettingTypes','level','id');
}
有很多定义
hasMany($related, $foreignKey, $localKey)
如果您可以为相关列使用更好的命名约定,例如
check\u id
,而不是level
等等,则更好。如果level
表的列设置类型
保留了对check table的引用,则您的映射将是
public function settingsval(){
return $this->belongsTo('YourCheckModelname','level');
}
并且在表中检查模型是否有如下映射
public function settingtype(){
return $this->hasMany(\App\AppSettingTypes::class);
}
Like wise如果表的type
列setting
保存了对settingtype表的引用,那么您的映射如下
public function settings(){
return $this->belongsTo('YourSettingTypeModelname','type');
}
而在设置类型中
模型有如下映射
public function settingsType(){
return $this->hasMany('YourSettingModelName'::class);
}
然后你必须在你的控制器里做
CheckModel::with('settingsval') //get all settingvalues
CheckModel::with('settingsval')现在为我提供最多appsetting类型,但我现在希望获得的设置不是appsettingtypes@GEOFFREYMWANGI如果还需要设置,则将零件更新为CheckModel::with('settingsval.settingsval')