我正在尝试在查询中传递一个变量。但出现错误:“ undefined variable $month
”
这是我的 Controller 部分
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{
}
最佳答案
得到了我的答案。我只需要在查询中传递 $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);
关于php - Laravel5.0 : while passing variable in query function, 它显示 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37086022/