问题描述
所以我正在使用Yajra数据表来制作我的数据表。
我在下面添加了另外一列,以便可以显示关系中的用户名。
->addColumn('customer_name',function (Order $order){
return $order->user->name;
})
尽管此列出现在数据表中,但我无法搜索它。下面是完整的功能代码:
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);
}
下面是Ajax代码:
<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>
我如何才能搜索到customer_name列?
解决方法
尝试快速加载:
$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'];
})
,
您必须输入表格名称和列。
示例:名称:“ users.id”
我们使用这种方法在同一系统中搜索ajax
或简单代码:
$objects = Model::query()
->leftJoin('curs','cur.id','=','model.cur_id')
->select([
'models.id','models.address','models.tag','curs.title_en',]);
return Datatables::of($objects)
->addColumn('actions',function ($model) {
return 'action'
})
->make(true);