2

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'},
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • What are the name and surname database columns called? – Stormhammer Jan 07 '19 at 14:49
  • it s in french (prenom , nom)=>(surname and name) i want to combine them in same coloumn@Stormhammer – haffis asma Jan 07 '19 at 14:50
  • @haffis, you could preprocess the JSON object and make the necessary changes before initializing the datatable. If the JSON object is only available as an AJAX response, then you can to that in Javascript. And if you have full control over the backend code that is generating the JSON (in PHP), then you can do it there to... for example, the DB query can be adjusted to return the query to make it have combined field. – asiby Jan 07 '19 at 14:51
  • i try that but i doesn't work for me can you help me please?@asiby – haffis asma Jan 07 '19 at 14:52
  • Sure. But I need some additional info. Do you have access to the DB query? – asiby Jan 07 '19 at 15:20
  • Also, which datatable are you working with? Are you manipulating JavaScript code and using jQuery or are you doing it in PHP? – asiby Jan 07 '19 at 15:32
  • If using jQuery, then you can find some useful information at https://datatables.net/examples/advanced_init/column_render.html. It shows how you can use the `columnDefs` and the `render` properties to define a column that can be a combination of any other columns even with some calculation if you want. The example shows how to replace column 0 (the name) with the name followed by the age (hidden column 3) surrounded by parenthesis. – asiby Jan 07 '19 at 15:39
  • @asiby i am using with PHP with function __invoke public function __invoke(ManagePatientRequest $request) { return Datatables::of($this->patient->getForDataTable()) ->escapeColumns(['id']) ->addColumn('nom_patient', function ($patient) { return $patient->nom_patient; }) ->addColumn('prenom_patient', function ($patient) { return $patient->prenom_patient; }) – haffis asma Jan 08 '19 at 06:56
  • I see. But with the small bits and pieces your have provided so far, it's impossible for me to provide any useful help. I give up. – asiby Jan 09 '19 at 03:22

2 Answers2

1

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

Stormhammer
  • 497
  • 4
  • 12
0

i follow your help and i try to combine theme with that

public function __invoke(ManagePatientRequest $request)
    {
        return Datatables::of($this->patient->getForDataTable())
            ->escapeColumns(['id'])
            ->addColumn('nom_patient', function ($patient) {
                return $patient->nom_patient.''.$patient->prenom_patient;
            }