编辑:为清楚起见,这是 Laravel 5.8。
这是我正在开发的 HR 应用。
他们要求一份报告,以显示打卡时间迟到的人。当然,我认为没问题。
所以我有一个表单,其中包含用户可以输入的一些自定义参数、开始日期、结束日期、工资和一系列部门。
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'));
}
所以,我的问题出在我的急切加载上,我想不通。如果我在员工预加载中取消注释过滤器,在代码块末尾附近的过滤器 block 中,$i->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);
关于php - Laravel 预加载约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58631971/