How to search in custom or composite columns in Laravel DataTables?

I’m using Yajra\DataTables in Laravel for tabular data display. For regular database columns, the search functionality works perfectly, but when it comes to custom columns that don’t exist in the database or columns built from multiple database fields the search does not work. How can I enable search for these custom or composite columns in Laravel Yajra\DataTables?

    processing: true,
    serverSide: true,
    "ajax": '/vehicles',
    columns: [
        { data: 'customer', name: 'customer'  },
        { data: 'vehicle_number', name: 'vehicle_number' },
        { data: 'model_name', name: 'vehicle_models.model_name' }

Here the customer does not exist in the database. It is created from 2 columns customer_name and customer_id

->addColumn('customer', function($row){
    if ( empty($row->customer_id) ) return $row->customer_name;
    else return $row->customer_name . ' (' . $row->customer_id . ')';

It loads the DataTable but when I search it gives an error. How do I solve this problem?

To fix the search functionality for custom columns, you can use 2 methods filter or ‘filterColumn’ provided by Yajra\DataTables.

->filterColumn('customer', function($query, $value) {
    if ( !empty($value) ){
        $query->where('customer_name', 'like', '%' . $value . '%')
        ->orWhere('customer_id', 'like', '%' . $value . '%');

Now, your custom column should be searchable in Laravel Yajra\DataTables.