gpt4 book ai didi

php - 如何在 Laravel Yajra DataTables 中的关系列上编辑列

转载 作者:行者123 更新时间:2023-12-04 15:36:43 25 4
gpt4 key购买 nike

我将 Laravel 6 与 Yajra 数据表一起使用。

我正在 session 模型上创建“SessionDataTable”。
该模型具有患者关系

class Session extends Model
{

public function patient()
{
return $this->belongsTo(Patient::class);
}

这是列定义:
protected function getColumns()
{
return [
Column::computed('action')
->exportable(false)
->printable(false)
->width(60)
->addClass('text-center'),
Column::make('id')->title('N°'),
Column::make('protocol_id')->title('Protocole')->data('protocol.name'),
Column::make('patient_id')->title('Patient')->data('patient.firstname')->name('patient.firstname'),
Column::make('room_id')->title('Salle')->data('room.name'),
Column::make('user_id')->title('Planifié par')->data('user.name'),
Column::make('scheduled_at')->title('Planifié le'),
];
}

这是列编辑:
public function dataTable($query)
{
return datatables()
->eloquent($query)
->editColumn('action', function ($model) {
if(!Gate::check('gestion')) {
return '';
}
return view('components.buttons.mini', [
'icon' => 'edit-pencil',
'url' => route($this->route_edit ?: strtolower(class_basename($model)) . '.edit', $model)
]);

})
->editColumn('scheduled_at', function ($model) {
return $model->scheduled_at->format('d/m/Y H:i');
})->editColumn('patient_id', function ($model) {
return $model->patient->name;
})->editColumn('name', function ($model) {
return $model->patient->firstname.' '.$model->patient->lastname;
})->editColumn('user_id', function ($model) {
return $model->user->firstname;
});

}

问题是“editColumn('patient_id')”被忽略了。我在数据表中有一个patient_id 列,但它只包含列定义中定义的名字。 editColumn('name') 被完全忽略并且不会出现在数据表中。

目标是在 DT Cell 中显示患者全名,这是 Patient Model 的 name 属性。 name 属性不是数据库字段 , 这是一个 Laravel 属性,它连接 firstname 和 lastname db 字段
 // Model Patient
public function getNameAttribute()
{
return $this->firstname . ' ' .$this->lastname;
}

我现在什至不要求搜索或订购,我只能订购和搜索姓氏,但我确实需要显示全名。为什么 editColumn 被忽略?

这是数据表查询定义:
/**
* Get query source of dataTable.
*
* @param Session $model
* @return Builder
*/
public function query(Session $model)
{
return $model->newQuery()->with(['patient', 'room', 'user', 'protocol']);
}

最佳答案

好的,我发现自己:

如果您以这种方式在列定义中使用 ->data() :

    Column::make('patient_id')->title('Patient')->data('patient.firstname')->name('patient.firstname'),

您需要在 editColumn 中使用与数据中相同的名称:->data(),在可能的情况下:patient.firstname
->editColumn('patient.firstname', function ($model) {
return $model->patient->name;
})

关于php - 如何在 Laravel Yajra DataTables 中的关系列上编辑列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59509479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com