首页 文章

Laravel背包中的一对多(1-N)关系

提问于
浏览
0

我正在使用Laravel BackPack CRUD . 我在两个表之间有一个1-N(一对多)的关系:

  • a personne 属于一个 structure

  • 在一个 structure 中有很多 personnes

所以我有一个 personnes 表:

idPersonnes,name,firstname,idStructures

还有一个 structure 表:

idStructures,name,adress

在我的人物模型我有:

public function structure()
{
     return $this->belongsTo('App\Models\Structure', '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
        ]);

这很好用 . 当我编辑人物时,我有select2下拉菜单,我可以选择并保存结构 .

现在当我编辑一个结构时,我想要显示属于该结构的所有人物 . 在我的结构模型中,我有:

public function personnes()
{
    return $this->hasMany('App\Models\Personne', 'idStructures', 'idStructures');
}

在我的结构控制器中我有:

$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
        ]);

它不起作用 . 难道我做错了什么 ?谢谢你的帮助 .

1 回答

相关问题