I am using yajra datatable in a laravel project to generate a list. My problem is: when I join a table and try to use a raw query to extract a column, it becomes unsearchable.
My table code:
$AAEListData = Entrepreneur::leftjoin('users', 'users.id', 'entrepreneurs.user_id')
->where('entrepreneurs.user_id', Auth::user()->id)
->groupBy('entrepreneurs.id')
->orderBy('entrepreneurs.created_at','desc')
->select([
'entrepreneurs.*',
'users.id as user',
DB::raw('CONCAT(IFNULL(users.user_first_name,"")," ",IFNULL(users.user_middle_name,"")," ",IFNULL(users.user_last_name,"")) as user_name')
]);
return Datatables::of($AAEListData)
->addColumn('action', function ($AAEListData) {
$view_btn = '<a href="' . url('entrepreneur/advance-against-expense-info/edit/' . Encryption::encodeId($AAEListData->id)) .
'" class="btn btn-xs btn-primary open" target="_blank" ><i class="fa fa-folder-open"></i> Open</a>';
return $view_btn;
})
->rawColumns(['action'])
->make(true);
The problem starts with the column user_name.
columns: [
{data: 'name', name: 'name'},
{data: 'phone', name: 'phone'},
{data: 'status', name: 'status'},
{data: 'user_name', name: 'user_name'},
{data: 'action', name: 'action'},
],
Whenever I try to search something, an error says column entrepreneurs.user_name not found. That is because it is not in that table, it comes from a joined table users. Can yajra datatable not detect this? I have searched this issue on the net and most suggestions ask converting the query collection to an array or simple avoid DB::RAW altogether. Neither solution is optimal for my situation but I am willing to do it the hard way if no other choice remains. My question is: is there a simpler way of informing datatable about which column belongs in which table? N.B: no, simply doing users.user_name does not work. That is the first thing I tried, in both controller and blade, neither worked.