【发布时间】:2021-04-22 06:13:22
【问题描述】:
我正在开发 Laravel 8 项目,我将在其中创建人力资源管理系统,所以我现在很难报告员工出勤率。
我有 2 个模型
User.php 和 Attendance.php 也是 2 个控制器和模型之间的关系。
public function attendances()
{
return $this->hasMany(Attendance::class, 'user_id');
}
这是来自我的AttendanceController.php
public function index()
{
return view('frontend.attendances.index', [
'employees' => $this->userRepository->getAllUsers()
->with(['attendances' => function($query) {
$query->where('company_id', Auth::user()->company_id);
}])
->get()
]);
}
所以现在我必须显示包含当前月份所有日期的表格,并显示每个用户是否在工作。
我有这是我的summary.blade.php
@foreach($employees as $employee)
<tr>
<td>
<img class="img-fluid rounded-circle mr-2" width="30" height="30" src="{{ $employee->image ? $employee->image : asset('assets/images/user.jpg') }}" alt="{{ $employee->firstname }}" data-original-title="{{ $employee->firstname }}">
{{ $employee->name() }}
</td>
@php($count=0)
@for($i=1; Carbon\Carbon::now()->daysInMonth >= $i; $i++)
@foreach($employee->attendances as $attendance)
@if($i == Carbon\Carbon::createFromFormat('Y-m-d H:m:s', $attendance->check_in)->isoFormat('D'))
<td class="text-center"><i class="fa fa-check text-success"></i></td>
@php($count++)
@else
<td class="text-center"><i class="fa fa-times text-danger"></i></td>
@endif
@endforeach
@endfor
<td class="text-success text-center">{{ $count }} / {{ Carbon\Carbon::now()->daysInMonth }}</td>
</tr>
@endforeach
这不是它应该如何工作的方式,但我已经尝试了很多方式,但仍然找不到合适的方式。 这是我想要实现的目标以及我的代码所拥有的图片
【问题讨论】:
-
有什么问题?它应该显示什么?
-
它应该显示图像上的数据。使用当前循环,它会为每个出席者继续
<td>...基本上循环从 1 月 1 日到 31 日,只显示一个出席者,之后它再次继续并显示第二个出席者等等......所以我的 1 月没有t 在第 31 天结束......它将与我的出席人数一样多(31 x 出席人数)我已经用图片更新了问题,我想要实现什么以及我拥有什么 -
我认为这是两个问题的组合:1)您可能没有相关月份的每一天的
Attendance和 2)您正在加载$employee->attendances关系中的每个出席(即使是那些与当月无关的)。要确定,需要更多关于Attendance模型的详细信息(迁移、强制转换)
标签: php arrays laravel laravel-blade laravel-collection