Laravel 5.4查询构建器在使用orWhere子句时缺少参数2

Yea*_*eak 4 php mysql laravel laravel-5 laravel-query-builder

有人可以告诉我为什么我得到这个错误以及如何解决它请.

    $lastDayPreviousMonth = date("Y-m-d", strtotime("last day of previous month"));
    $firstDayPreviousMonth = date("Y-m-d", strtotime("first day of previous month"));

    $query = DB::table('employees')
        ->where('Emp_ClientId', '=', $clientId)
        ->where('Emp_StatusId', 1)
        ->orWhere(function ($query, $firstDayPreviousMonth, $lastDayPreviousMonth){
            $query->where('Emp_DateSuspTerm', '>=', $firstDayPreviousMonth)
                ->where('Emp_DateSuspTerm', '<=', $lastDayPreviousMonth)
                ->where('Emp_ClientId', '=', $clientId);
        })
        ->count();
Run Code Online (Sandbox Code Playgroud)

我运行时遇到以下错误

App\Http\Models\Employees :: App\Http\Models {closure}()缺少参数2

我认为它与第一天的previousmonth和lastdaypreviousmonth参数一起传递到orWhere子句 - 如果我把它拿出来我得到未定义的变量.

iCo*_*ers 5

您可以使用use关键字来使用闭包

$query = DB::table('employees')
            ->where('Emp_ClientId', '=', $clientId)
            ->where('Emp_StatusId', 1)
            ->orWhere(function ($query) use($firstDayPreviousMonth, $lastDayPreviousMonth,$clientId){
                $query->where('Emp_DateSuspTerm', '>=', $firstDayPreviousMonth)
                    ->where('Emp_DateSuspTerm', '<=', $lastDayPreviousMonth)
                    ->where('Emp_ClientId', '=', $clientId);
            })
            ->count();
Run Code Online (Sandbox Code Playgroud)