0

在 yajra/datatables 中有没有办法过滤和排序不在数据库中的数据?我创建了一个带有 addColumn 的列,它可以在 2 个日期之间产生差异,它工作正常,但我无法使用 DataTable 的搜索和排序功能。有好心人可以给我小费吗?

这是Controller中的源代码

public function TasksData(){


    return DataTables::of(Task::query())
           ->editColumn('id', function(Task $task) {
                $link='';   
                $link .= '<a href="#">'.$task->id.'</a>';
                return $link;
            })
           ->editColumn('task_id', function(Task $task) {
                return $task->Task->name;
            })
           ->editColumn('reception_date', function(Task $task) {
               $date  = '';
               $date .= (new Carbon\Carbon($task->reception_date))->format('d/m/Y'); 
               return $date;
            })
           ->filterColumn('reception_date', function ($query, $keyword) {
               $query->whereRaw("DATE_FORMAT(reception_date,'%d/%m/%Y') like ?", ["%$keyword%"]);
            })
           ->editColumn('fullfilment_request', function(Task $task) {
               $date  = '';
               $date .= (new Carbon\Carbon($task->fullfilment_request))->format('d/m/Y'); 
               return $date;
            })
           ->filterColumn('fullfilment_request', function ($query, $keyword) {
               $query->whereRaw("DATE_FORMAT(fullfilment_request,'%d/%m/%Y') like ?", ["%$keyword%"]);
            })
           ->addColumn('days', function(Task $task) {
               $date  = '';
               $reception = Carbon\Carbon::parse($task->reception_date);
               $fullfilment = Carbon\Carbon::parse($task->fullfilment_request);
               $date .= $reception->diffInDays($fullfilment, false); 
               return $date;
            })
           ->setRowClass(function (Task $task) {
               $date  = '';
               $reception = Carbon\Carbon::parse($task->reception_date);
               $fullfilment = Carbon\Carbon::parse($task->fullfilment_request);
               $date .= $reception->diffInDays($fullfilment, false);
               return ($date < 7 && $date == 1)  ? 'bg-warning' : (($date < 0 && $date < 3) ? 'bg-danger' : '');
            })
           ->rawColumns(['id','task_id','reception_date','fullfilment_request','days'])
           ->make(true);

}
4

0 回答 0