2017-09-26 79 views
2

有人可以告訴我爲什麼即時通訊出現此錯誤並請如何解決此問題。Laravel 5.4查詢生成器使用orWhere子句時丟失參數2

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

即時得到以下錯誤,當我運行這個

缺少參數2爲App \ HTTP \型號\員工::應用程序\ HTTP \ {模式關閉}()

我認爲這與第一天前一個月和最後一天前一個月的參數im傳遞到orWhere子句 - 如果我把它拿出來,我得到未定義的變量。

回答

3

您CA n使用封閉使用用關鍵字

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

如果有用的標記答案接受 – iCoders

+0

我會當定時器允許:) – Yeak

相關問題