So i am using Yajra Datatables to make my datatable.
I have added a additional column like below so it can show the users name from a relationship.
->addColumn('customer_name', function (Order $order){
return $order->user->name;
})
Although this column appears in the datatable I am unable to search it. Below is the full function code:
public function viewDTPending(){
$store= Auth::user()->store;
$pending = Order::where('store', $store)->where('status', 'Pending')->orderBy('user_id')->groupBy('order_id', 'status', 'order_branch', 'user_id', 'created_at', 'updated_at');
return Datatables::of($pending)
->addColumn('customer_name', function (Order $order){
return $order->user->name;
})
->editColumn('created_at', function (Order $order){
return $order->created_at->diffForHumans();
})
->make(true);
}
And below is the ajax code:
<script>
$(function() {
$('#table').DataTable({
processing: true,
serverSide: true,
ajax: '{{ url('getPendingOrdersDT') }}',
columns: [
{ data: 'order_id', name: 'order_id' },
{ data: 'user_id', name: 'user_id' },
{ data: 'customer_name', name: 'customer_name' },
{ data: 'status', name: 'status' }
]
});
});
</script>
How could i get it to search the customer_name column?
Try Eager loading :
$pending = Order::with('user:id,name')->where('store', $store)->where('status', 'Pending')->orderBy('user_id')->groupBy('order_id', 'status', 'order_branch', 'user_id', 'created_at', 'updated_at');
return Datatables::of($pending)
->addColumn('customer_name', function ($row){
return $row->user['name'];
})