1

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?

2
  • line 78 is that the orWhere clause or the info()? Commented Oct 29, 2019 at 11:23
  • The line 78 is orWhere line. Commented Oct 29, 2019 at 11:23

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow. Fantastic! Thank you very much. I'll accept this answer 6min later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.