I got Users, Absences and Reasons tables:
Users
id | name
Absences
id | user_id:foreignKey | reason_id:foreignKey | deputy_id:foreignKey | start:date | end:date
Reasons
id | name
I got attendance calendar which looks like:
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 |
-----------------------------------------------|
Where "V" is Vacation, "S" sick etc. I want to print each User attendance report in that 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
Also I want to have a tooltip, where I print specific data for each day (reason name, deputy name).
I have no idea how to get the range between two dates in each absence, merge it to got f.e table of user absence days with another array with reason.:
[
2 => ['reason_id => 1, deputy_id = 3],
3 => ['reason_id => 1, deputy_id = 3],
7 => ['reason_id => 2, deputy_id = 1]
]
Where keys [2,3,7] are the days of the absence.
Is there easier way to achieve that or should I rebuild the database structure?
CodePudding user response:
Here is my working solution :
<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>
I also created repo for you so you can test this easy.