我正在尝试在查询中传递变量。但是会发生错误:"未定义的变量$month
"
这是我的控制器部分
public function due(Request $request){
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
$month = $request->month;
if(isset($month)) {
try {
$val=DB::connection()->getDatabaseName();
if(DB::connection()->getDatabaseName()) {
$month=$month;
$bRecord=DB::table('clients')->whereNotIn('ClientID', function($q){
$q->select('ClientID')->from('bills')
->where(function ($query) use($month) { //it shows $month is undefined, but why ??
$query->whereMonth('Date', '=', $month);
});
})->paginate(10);
return view('bills.dueRecord')->with('bRecord', $bRecord);
}else{
$er="/connection status: database error";
return view('home')->with('error',$er); //'error' is passed to home
}
} catch (\Exception $e){
$er="/connection status: database error";
return view('errors/503')->with('error',$er);
}
}
}else{
}
答案 0 :(得分:0)
得到了我的答案。我只需要在查询中两次传递$ month的范围。
$bRecord=DB::table('clients')->whereNotIn('ClientID', function($q) use($month){
$q->select('ClientID')->from('bills')
->where(function ($query) use($month) {
$query->whereMonth('Date', '=', $month);
});
})->paginate(10);