2015-05-04 83 views
-2

我有一個表格來標記員工的出勤情況,HR填寫在每個當前員工的循環中。我需要它顯示數據庫中出席考勤的當前值,否則它應該是空白的。Foreach循環沒有正確地通過

在我控制我查詢了已有的結果:

$results = Attendance::where('Sdate', '=', date("Y-m-d", strtotime($TheDate))) ->get();

然後依次通過他們獲得的員工詳細信息:

foreach($results as $result) 
{ 
    $contractors = DB::table('contractors') ->where('contractors.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $employees = DB::table('current_employees') ->where('current_employees.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $showEmployees = array_merge($contractors, $employees); 
} 

這應該排除誰擁有保存在記錄中的所有員工參加該日期,但它似乎沒有正確循環。它會排除一些結果,但不是全部。如果我返回結果變量,我會得到正確的記錄列表,所以我知道該部分工作正常。

我正在尋找在我看來實現的是一樣的東西:

@foreach($attendance as $results) 

Show form where there's an existing record for this date and department 

@endforeach 



@foreach($employees as $employee) 

Show form for all employees in this department (but should exclude results where there is a record in attendance) 

@endforeach 

回答

0

的問題與您的代碼是要保存結果的變量,而不是在數組中。 您的解決方案是將數據存儲在陣列

foreach($results as $result) 
{ 
    $contractors[] = DB::table('contractors') ->where('contractors.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $employees = DB::table('current_employees') ->where('current_employees.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $showEmployees = array_merge($contractors, $employees); 
} 

嘗試打印承包商陣列,看看happens.I希望這個作品

0

這是替我回答了關於Laracasts。

我需要做什麼是創建變量列表對證(員工數量)

$PRNs = Attendance::where('Sdate', '=', date("Y-m-d", strtotime($TheDate)))->lists('PRN'); 

然後使用Laravel的whereNotIn,覈對清單。

$contractors = DB::table('contractors') 
     ->where('contractors.AreaPosition', '=', $department) 
     ->whereNotIn('PRN', $PRNs) 
     ->get();