Search code examples
phplaraveldatatablelaravel-bladelaravel-translatable

Combine Two Columns From DataTable


enter image description here

I want to combine two columns in my DataTable. I have name and surname but I want to combine them and show just one column with the full name.

Blade

<th>{{ trans('labels.backend.patients.table.id') }}</th>
<th>{{ trans('labels.backend.patients.table.nom_patient') }}</th>
<th>{{ trans('labels.backend.patients.table.prenom_patient') }}</th>
<th>{{ trans('labels.backend.patients.table.date_naissance') }}</th>

DataTable Ajax

columns: [ {data: 'id', name: '{{config('module.patients.table')}}.id'},
    {data: 'nom_patient', name: '{{config('module.patients.table')}}.nom_patient'},
    {data: 'prenom_patient', name: '{{config('module.patients.table')}}.prenom_patient'},

Solution

  • In your case I would create an accessor in your model:

    getNomCompletAttribute() {
        return $this->prenom . ' ' . $this->nom;
    }
    

    I believe you can now just call nom_complet like it would be a regular field in the datatables.

    Docs: https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor