简体   繁体   English

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

[英]Laravel 5.4 query builder Missing argument 2 when using orWhere clause

Can someone tell me why im getting this error and how to fix it please. 有人可以告诉我为什么我得到这个错误以及如何解决它请。

    $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();

Im getting the following error when i run this 我运行时遇到以下错误

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

I think it has to do with the firstdaypreviousmonth and lastdaypreviousmonth parameters im passing into the orWhere clause - if i take it out i get undefined variable. 我认为它与第一天的previousmonth和lastdaypreviousmonth参数一起传递到orWhere子句 - 如果我把它拿出来我得到未定义的变量。

You ca n use closure using use keyword 您可以使用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM