如何为Laravel背包显示crud中链接模型的字段



我有两个通过透视表连接的模型Users和Roles。我想在CrudUserController中显示角色模型中的一个字段。例如用户型号:

public function roles()
{
return $this->belongsToMany( Role::class, 'role_user', 'user_id', 'role_id');
}

角色模型

public function user()
{
return $this->belongsToMany( User::class, 'role_user','role_id','user_id');
}

UserCrudController

$this->crud->addFields([
[
'type' => 'repeatable',
'label' => 'Role',
'name' => 'roles', <- this field shows select2 with a list of roles
'subfields' => [
[
'name'  => 'description', <- this field is from the role model, it should change when changing the role in select2 
'label' => 'Description',
'type'  => 'text',
]
]
]
])

我不确定这是否可能,因为通常这样的操作都是使用js 实现的

如果您想创建内联条目,我建议使用InlineCreateOperation,它也可以在BelongsToMany字段中工作:https://backpackforlaravel.com/docs/5.x/crud-operation-inline-create

干杯

最新更新