我有一张表格来标记员工'人力资源部门填写的每个现有员工的出勤情况。我需要它来显示数据库中标记出勤的当前值,否则它应该是空白的。
在我的控制器中,我查询现有结果:
$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 :(得分: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);
}
尝试打印承包商阵列,看看会发生什么。我希望这有效
答案 1 :(得分: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();