Laravel 5.4 query builder Missing argument 2 when using orWhere clause

Viewed 651

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}()

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.

1 Answers

You ca n use closure using use keyword

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