Reputation: 449
In my Laravel controller, it keeps saying error like this.
[2019-10-29 11:13:57] local.ERROR: Undefined variable: target_date
{"userId":2,"exception":"[object] (ErrorException(code: 0): Undefined variable: target_date at
/home/ljw/public_html/byappscms/app/Http/Controllers/ChartController.php:78)
However, in my controller file, the variable EXIST.
ChartController.php
public function onGetAppDailyChartData(Request $request)
{
info("~~~~~~~~~~~" . $request->date);
$target_date = strtotime($request->date);
info("~~~~~~~~~~~" . $target_date);
$appsTotal = AppsData::where('app_process', '=', '7')
->where(function ($query) {
$query->where('service_type', '=', 'lite')
->orWhere('end_time', '>', $target_date);
})
->count();
}
And I checked the log, the values printed well. They printed the date and the UNIX timestamp.
What possibly cause this error here?
Upvotes: 1
Views: 1018
Reputation: 11034
The variable is outside the scope of closure functions, introduce it with use
$appsTotal = AppsData::where('app_process', '=', '7')
->where(function($query) use ($target_date) { // <--- Here
$query->where('service_type', '=', 'lite')
->orWhere('end_time', '>', $target_date);
})
->count();
Hope this helps
Upvotes: 4