【发布时间】:2019-10-15 15:55:12
【问题描述】:
在 Laravel Backpack 中使用 select2 字段/列类型时,列表视图显示外部实体的“id”而不是所需的外键(在本例中为 Session 的“名称”)。
Laravel 5.8.4,背包 3.4。我在 GitHub 上询问,得到的答复是我的模型中的关系不正确。我认为这不是问题,因为名称会加载到编辑视图中。
GradeCrudController
$this->crud->addColumn([
'label' => "Session",
'type' => 'select2',
'name' => 'session_id', // the db column for the foreign key
'entity' => 'session', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'model' => "App\Models\Session" // foreign key model
]);
等级(型号)
public function session()
{
return $this->belongsTo('App\Models\Session');
}
会话(模型)
public function grades()
{
return $this->hasMany('App\Models\Grade');
}
【问题讨论】: