Ad

How To EditColumn On A Relationship Column In Laravel Yajra DataTables

I'm using Laravel 6 with Yajra Datatable.

I'm creating a "SessionDataTable" on a Session Model. This model has a Patient relationship

class Session extends Model
{

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

Here is the Column Definition :

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'),
    ];
}

here is the Column edit :

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;
        });

}

The problem is this "editColumn('patient_id')" is ignored. I have a patient_id column in the datatable, but it contains only the firstname, as defined in the column definition. The editColumn('name') is completely ignored and doesn't appears in the datatable.

The goal is to display the patient fullname in the DT Cell, which is the name attribute of the Patient Model. The name attribute is not a db field, it's a laravel attribute which concat firstname and lastname db field

 // Model Patient
 public function getNameAttribute()
 {
    return $this->firstname . ' ' .$this->lastname;
 }

I'm not even asking for search or ordering for now, I can order and search only on lastname, but I really need to display the fullname. Why is the editColumn being ignored ?

Here is the Datatable Query Definition :

/**
 * Get query source of dataTable.
 *
 * @param Session $model
 * @return Builder
 */
public function query(Session $model)
{
    return $model->newQuery()->with(['patient', 'room', 'user', 'protocol']);
}
Ad

Answer

Ok, I found myself :

If you are using the ->data() in the column definition this way :

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

You need to use the same name in the editColumn as the one in the data : ->data(), in may case : patient.firstname

->editColumn('patient.firstname', function ($model) {
    return $model->patient->name;
})
Ad
source: stackoverflow.com
Ad