我正在使用带有Yajra数据表的Laravel 6。
我正在会话模型上创建“ 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单元中显示患者全名,这是“患者模型”的名称属性。 name属性不是db字段,它是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']);
}
答案 0 :(得分:0)
尝试一下
return Datatables::of($model)
->editColumn('name', function ($orders) {
return $model->patient->firstname.' '.$model->patient->lastname;
})->make(true);
OR
return Datatables::of($model)
->editColumn('name', function ($orders) {
return $model->patient->name;
})->make(true);
OR
use DataTables;
$model = App\User::query();
return DataTables::eloquent($model)
->editColumn('name', function(Patient $patient) {
return $patient->firstname.' '.$patient->lastname;
})
->toJson();
});
答案 1 :(得分:0)
好的,我发现自己:
如果您以这种方式在列定义中使用-> 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;
})