我正在使用 laravel 和数据表版本的详细信息:
| Laravel 5.5 |
| Yajra/datatables 8* |
当我尝试查询数据
时,我总是遇到此错误"message": "Method where does not exist.",
"exception": "BadMethodCallException",
我的代码在下面
public function getExpensesHistory(Request $request){
$data = Expenses::with(['expenses' => function ($query) {
$query->select('id', 'type_name');
},'user' => function ($query) {
$query->select('id', 'name');
}])->orderBy('exp_date','desc')->get();
$datatables=DataTables::of($data);
$from_date = date('Y-m-d',strtotime($datatables->request->get('from_date')));
$end_date = date('Y-m-d',strtotime($datatables->request->get('end_date')));
if ($end_date and $from_date) {
$datatables->where('exp_date','<=',$end_date)->where('exp_date','>=',$from_date);
}
return $datatables->make(true);
}
答案 0 :(得分:0)
您的查询应该像
一样$query = Expenses::query();
$query->with(['expenses' => function ($query) {
$query->select('id', 'type_name');
},'user' => function ($query) {
$query->select('id', 'name');
}]);
$from_date = date('Y-m-d',strtotime($request->from_date));
$end_date = date('Y-m-d',strtotime($request->end_date));
if ($end_date and $from_date) {
$query->where(function($q) use ($from_date, $end_date){
$q->where('exp_date','>=',$from_date);
$q->where('exp_date','<=',$end_date);
});
}
$data = $query->orderBy('exp_date','desc')->get();
在这里,我在变量中进行查询,并在该对象上执行所有操作