Laravel背包中的一对多(1-N)关系
我正在使用。我在两个表之间有一个1-N(一对多)关系:Laravel背包中的一对多(1-N)关系,laravel,backpack-for-laravel,Laravel,Backpack For Laravel,我正在使用。我在两个表之间有一个1-N(一对多)关系: 人员属于一个结构 在结构中有许多人员 因此,我有一个人员表: IDpersones,name,firstname,idStructures 以及结构表: 结构、名称、地址 在我的个人模型中,我有: public function structure() { return $this->belongsTo('App\Models\Structure', 'idStructures', 'idStructures'); }
- 人员属于一个结构
- 在结构中有许多人员
public function structure()
{
return $this->belongsTo('App\Models\Structure', 'idStructures', 'idStructures');
}
public function personnes()
{
return $this->hasMany('App\Models\Personne', 'idStructures', 'idStructures');
}
在我的人事控制器中,我有:
$this->crud->addField([
'label' => 'Structure',
'type' => 'select2',
'name' => 'idStructures', // the db column for the foreign key
'entity' => 'structure', // the method that defines the relationship in your Model
'attribute' => 'nom', // foreign key attribute that is shown to user
'model' => 'App\Models\Structure' // foreign key model
]);
$this->crud->addField([
'label' => 'Personnes',
'type' => 'select2',
'name' => 'idStructures', // the db column for the foreign key
'entity' => 'personnes', // the method that defines the relationship in your Model
'attribute' => 'nom', // foreign key attribute that is shown to user
'model' => 'App\Models\Personne' // foreign key model
]);
这很有效。当我编辑人员时,我有select2下拉列表,我可以选择并保存结构
现在,当我编辑一个结构时,我想显示属于该结构的所有人员。
在我的结构模型中,我有:
public function structure()
{
return $this->belongsTo('App\Models\Structure', 'idStructures', 'idStructures');
}
public function personnes()
{
return $this->hasMany('App\Models\Personne', 'idStructures', 'idStructures');
}
在我的结构控制器中,我有:
$this->crud->addField([
'label' => 'Structure',
'type' => 'select2',
'name' => 'idStructures', // the db column for the foreign key
'entity' => 'structure', // the method that defines the relationship in your Model
'attribute' => 'nom', // foreign key attribute that is shown to user
'model' => 'App\Models\Structure' // foreign key model
]);
$this->crud->addField([
'label' => 'Personnes',
'type' => 'select2',
'name' => 'idStructures', // the db column for the foreign key
'entity' => 'personnes', // the method that defines the relationship in your Model
'attribute' => 'nom', // foreign key attribute that is shown to user
'model' => 'App\Models\Personne' // foreign key model
]);
它不起作用了。
我做错什么了吗?
谢谢您的帮助。此处有相同的问题和答案:此处有相同的问题和答案: