为团队准备数组时出现问题
Problem with preparing array with attendance for team
我得到了用户、缺勤和原因 tables:
Users
id | name
Absences
id | user_id:foreignKey | reason_id:foreignKey | deputy_id:foreignKey | start:date | end:date
Reasons
id | name
我得到了如下所示的考勤日历:
Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
-----------------------------------------------|
User1 | o | o | V | V | S | S | o | o | o | V |
-----------------------------------------------|
User2 | T | o | o | o | H | V | o | V | o | V |
-----------------------------------------------|
其中“V”表示假期,“S”表示生病等。我想打印其中的每个用户出勤报告table。
@foreach ($users as $user )
<tr>
<td> {{ $user->name }} </td>
@if($user->absences != null)
@foreach($days as $day)
@foreach ($user->absences as $absence)
@if($day >= $absence->start->format('d') && $day <= $absence->end->format('d'))
// there check if $user->absence->reason_id == something ...
@else
@endif
@endforeach
@endforeach
@endif
</tr>
@endforeach
我还想要一个工具提示,我在其中打印每天的特定数据(原因名称、代理名称)。
我不知道如何在每次缺席时获取两个日期之间的范围,将其合并以得到 f.e table 的用户缺席天数与另一个有原因的数组。:
[
2 => ['reason_id => 1, deputy_id = 3],
3 => ['reason_id => 1, deputy_id = 3],
7 => ['reason_id => 2, deputy_id = 1]
]
其中键 [2,3,7] 是缺席的天数。
是否有更简单的方法来实现,或者我应该重建数据库结构?
这是我的工作解决方案:
<table>
<thead>
<th>Day</th>
@foreach (App\Models\User::all() as $user)
<th>{{ $user->name }}</th>
@endforeach
</thead>
<tbody>
@for ($i = 1; $i <= Carbon\Carbon::now()->daysInMonth; $i++ )
<tr>
<td> {{ $i }} </td>
@foreach (App\Models\User::with('absence')->get() as $user)
<td>
@foreach ( $user->absence as $absence )
@if ($i>= Carbon\Carbon::parse($absence->start)->day && $i<=Carbon\Carbon::parse($absence->end)->day)
<span>{{$absence->reason->symbol}} - {{ $absence->deputy->name }}</span>
@endif
@endforeach
</td>
@endforeach
</tr>
@endfor
</tbody>
</table>
我还为您创建了存储库,因此您可以轻松进行测试。
- git 克隆 https://github.com/djokicpn/vacation-calendar vacation-project
- npm 安装
- 作曲家安装
- php artisan key:generate
- php artisan migrate:fresh --seed
- php artisan 服务
我得到了用户、缺勤和原因 tables:
Users
id | name
Absences
id | user_id:foreignKey | reason_id:foreignKey | deputy_id:foreignKey | start:date | end:date
Reasons
id | name
我得到了如下所示的考勤日历:
Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
-----------------------------------------------|
User1 | o | o | V | V | S | S | o | o | o | V |
-----------------------------------------------|
User2 | T | o | o | o | H | V | o | V | o | V |
-----------------------------------------------|
其中“V”表示假期,“S”表示生病等。我想打印其中的每个用户出勤报告table。
@foreach ($users as $user )
<tr>
<td> {{ $user->name }} </td>
@if($user->absences != null)
@foreach($days as $day)
@foreach ($user->absences as $absence)
@if($day >= $absence->start->format('d') && $day <= $absence->end->format('d'))
// there check if $user->absence->reason_id == something ...
@else
@endif
@endforeach
@endforeach
@endif
</tr>
@endforeach
我还想要一个工具提示,我在其中打印每天的特定数据(原因名称、代理名称)。
我不知道如何在每次缺席时获取两个日期之间的范围,将其合并以得到 f.e table 的用户缺席天数与另一个有原因的数组。:
[
2 => ['reason_id => 1, deputy_id = 3],
3 => ['reason_id => 1, deputy_id = 3],
7 => ['reason_id => 2, deputy_id = 1]
]
其中键 [2,3,7] 是缺席的天数。
是否有更简单的方法来实现,或者我应该重建数据库结构?
这是我的工作解决方案:
<table>
<thead>
<th>Day</th>
@foreach (App\Models\User::all() as $user)
<th>{{ $user->name }}</th>
@endforeach
</thead>
<tbody>
@for ($i = 1; $i <= Carbon\Carbon::now()->daysInMonth; $i++ )
<tr>
<td> {{ $i }} </td>
@foreach (App\Models\User::with('absence')->get() as $user)
<td>
@foreach ( $user->absence as $absence )
@if ($i>= Carbon\Carbon::parse($absence->start)->day && $i<=Carbon\Carbon::parse($absence->end)->day)
<span>{{$absence->reason->symbol}} - {{ $absence->deputy->name }}</span>
@endif
@endforeach
</td>
@endforeach
</tr>
@endfor
</tbody>
</table>
我还为您创建了存储库,因此您可以轻松进行测试。
- git 克隆 https://github.com/djokicpn/vacation-calendar vacation-project
- npm 安装
- 作曲家安装
- php artisan key:generate
- php artisan migrate:fresh --seed
- php artisan 服务