I have a hospital table and schedule table, and i want to create a schedule table html using these table
+----+--------------+
| id | name |
+----+--------------+
| 1 | hospital a |
| 2 | hospital b |
+----+--------------+
+-----+---------+--------+---------------+
| day | start | end | hospital_id |
+-----+---------+--------+---------------+
| mon | 10:00 | 14:00 | 1 |
| tue | 15:00 | 20:00 | 1 |
| wed | 14:00 | 19:00 | 2 |
| fri | 10:00 | 15:00 | 2 |
+-----+---------+--------+---------------+
the problem is the schedule time didn't align to the correct <th>
What i need:
+-------+------+--------+-------+-------+-------+------+
| mon | tue | wed | thu | fri | sat | sun |
+------------------------------------------------------+
| hospital a |
+-------+-------+-------+-------+-------+-------+------+
| 10:00 | 15:00 | | | | | |
| - | - | | | | | |
| 14:00 | 20:00 | | | | | |
+-------+-------+-------+-------+-------+-------+------+
| hospital b |
+-------+-------+-------+-------+-------+-------+------+
| | | 14:00 | | 10:00 | | |
| | | - | | - | | |
| | | 19:00 | | 15:00 | | |
+-------+-------+-------+-------+-------+-------+------+
but it become like this
+-------+-------+-------+-------+-------+-------+------+
| mon | tue | wed | thu | fri | sat | sun |
+------------------------------------------------------+
| hospital a |
+-------+------+--------+-------+-------+-------+------+
| 10:00 | 15:00 | | | | | |
| - | - | | | | | |
| 14:00 | 20:00 | | | | | |
+-------+-------+-------+-------+-------+-------+------+
| hospital b |
+-------+-------+-------+-------+-------+-------+------+
| 14:00 | 10:00 | | | | | |
| - | - | | | | | |
| 19:00 | 15:00 | | | | | |
+-------+-------+-------+-------+-------+-------+------+
this is my .blade.php script
<table class="table">
<thead>
<tr>
<td class="text-center">mon</td>
<td class="text-center">tue</td>
<td class="text-center">wed</td>
<td class="text-center">thu</td>
<td class="text-center">fri</td>
<td class="text-center">sat</td>
<td class="text-center">sun</td>
</tr>
</thead>
<tbody>
@foreach($hospital as $hos)
<tr>
<td class="text-center _b" colspan="7">{{$hos->name}}</td>
</tr>
<tr>
@foreach($schedule->where('hospital_id', $hos->id) as $sch)
@if($sch->day == 'mon')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam active">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'tue')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'wed')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'thu')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'fri')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'sat')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam active">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'sun')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@endforeach
</tr>
@endforeach
</tbody>
</table>
</table>
this is happen because the <td></td>
will not created when the if
condition doesn't meet. but when I try to move if
inside the td
the table looks more terrible
You need to output an empty <td></td>
should that day doesn't have any time.
<?php
$days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
?>
@foreach($schedule->where('hospital_id', $hos->id) as $sch)
@foreach($days as $day)
@if($sch->day == $day)
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam active">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@else
<td></td>
@endif
@endforeach
@endforeach