-2

我有一个表格来标记员工的出勤情况,HR 填写该表格会覆盖每个当前员工。我需要它来显示数据库中标记出勤的当前值,否则它应该是空白的。

在我的控制器中,我查询现有结果:

$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
4

2 回答 2

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

尝试打印承包商阵列,看看会发生什么。我希望这可行

于 2015-05-04T06:36:37.067 回答
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();
于 2015-05-04T10:06:30.693 回答