Laravel 预加载约束问题

Laravel Eager Load Constraint issue

编辑:为清楚起见,这是 Laravel 5.8。

这是我正在开发的 HR 应用程序。

他们要求一份报告,以显示打卡迟到的人。当然,我认为没问题。

所以我有一个表单,其中包含用户可以输入的一些自定义参数,start_date、end_date、工资和一系列部门。

public function show()
{
    request()->validate([
        'start_date' => 'required|date|before_or_equal:today'
    ]);

    $start = Carbon::parse(request('start_date'));
    $end = request('end_date') ? Carbon::parse(request('end_date')) : today();
    $wage = request('wage');
    $departments = request('departments');

    $query = EmployeePunch::with([
        'employee' => function($query) use ($wage, $departments) {
            // IF I UN COMMENT THESE, IN THE FILTER BLOCK BELOW, THE EMPLOYEE BECOMES UNDEFINED.
            // if($wage != null) {
            //     $query->where('hourly', $wage);
            // }

            // if($departments) {
            //     $query->whereIn('department_id', $departments);
            // }
        },
        'employee.group',
        'employee.department'
    ])
    ->whereBetween('punch_time', [$start->startOfDay(), $end->endOfDay()])
    // only care about punch in for the day
    ->where('type', 1);

    $results = $query->get();

    $latePunches = $results->filter(function ($i) {
        $day = strtolower($i->punch_time->format('D'));
        $startTime = Carbon::parse(sprintf('%s %s', 
                                            $i->punch_time->format('d-m-Y'), 
                                            $i->employee->group[$day.'_start_time'])
                    );

        return $i->punch_time->isAfter($startTime) 
                && $i->punch_time->diffInMinutes($startTime) >= 5;
    });

    return view('hr.employeeLateReport.show', compact('latePunches'));
}

所以,我的问题出在我的急切加载上,我无法解决这个问题。如果我取消注释员工预加载中的过滤器,在代码块末尾附近的过滤器块中,$i->employee 变为未定义。如果省略过滤器,一切都很好。我检查了生成的查询,一切看起来都很棒。

如有任何帮助,我们将不胜感激。

关系方法如下

Employee.php

public function punches()
{
    return $this->hasMany(EmployeePunch::class);
}

public function group()
{
    return $this->belongsTo(Group::class);
}

public function department()
{
    return $this->belongsTo(Department::class)->withDefault();
}

EmployeePunch.php

public function employee()
{
    return $this->belongsTo(Employee::class);
}

SQL输出

尝试使用 whereHas 并嵌套 whereBetween:

$query = EmployeePunch::with([
    'employee' => function($query) use ($wage, $departments) {
        if($wage != null) {
            $query->where('hourly', $wage);
        }

        if($departments) {
            $query->whereIn('department_id', $departments);
        }
    },
    'employee.group',
    'employee.department'
])->whereHas('employee', function($q) use($start, $end) {
    $q->whereBetween('punch_time', [$start->startOfDay(), $end->endOfDay()]);
})->where('type', 1);