我有一份由人力资源部门填写的表格来标记员工的出勤情况,该表格会循环到每个当前员工身上。我需要它来显示数据库中标记出勤的当前值,否则它应该为空白。
在我的控制器中,我查询现有结果:
$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
您的代码的问题是您将结果保存在变量中而不是数组中。 您的解决方案是将数据存储在数组中
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);
}
尝试打印承包商数组,看看会发生什么。我希望这能起作用
这个问题已在 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();