Laravel-datatables: How can I merge two columns in just one?

Created on 31 Jul 2019  路  3Comments  路  Source: yajra/laravel-datatables

I'm using server side, and I have two fields, demand_number and demand_year, and I like to display this fields in one field like a demand_number / demand_year ( 001 / 2019 )

How can I do this?

  • Operating System: Linux Mint
  • PHP Version: 7.2
  • Laravel Version: 5.7
  • Laravel-Datatables Version: 9.4.1

Most helpful comment

Try to create a custom column like demand_info in your frontend. And, on your controller, edit the column, like:

$datatables->editColumn('demand_info', function ($eloquent) {
    return $eloquent->demand_number . '/' . $eloquent->demand_year;  
});

If you need search by this fields, don't forget to filter the column, like:

$datatables->filterColumn('demand_info', function ($query, $keyword) {
    $query->where('demand_number', 'like', $keyword)
               ->orWhere('demand_year', 'like', $keyword);
});

Warning: This codes was not tested.

All 3 comments

You can use accessor/mutator and add custom column

Try to create a custom column like demand_info in your frontend. And, on your controller, edit the column, like:

$datatables->editColumn('demand_info', function ($eloquent) {
    return $eloquent->demand_number . '/' . $eloquent->demand_year;  
});

If you need search by this fields, don't forget to filter the column, like:

$datatables->filterColumn('demand_info', function ($query, $keyword) {
    $query->where('demand_number', 'like', $keyword)
               ->orWhere('demand_year', 'like', $keyword);
});

Warning: This codes was not tested.

Thanks @connectmalves works perfectly!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

hari-web picture hari-web  路  3Comments

FilipeBorges1993 picture FilipeBorges1993  路  3Comments

ahmadbadpey picture ahmadbadpey  路  3Comments

vipin733 picture vipin733  路  3Comments