我有一张医院餐桌和日程表,我想用这些表
创建一个日程表html+----+--------------+
| 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 |
+-----+---------+--------+---------------+
问题是时间表时间与正确的<th>
对齐
我需要的是什么:
+-------+------+--------+-------+-------+-------+------+
| 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 | | |
+-------+-------+-------+-------+-------+-------+------+
但它变得像这样
+-------+-------+-------+-------+-------+-------+------+
| 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 | | | | | |
+-------+-------+-------+-------+-------+-------+------+
这是我的.blade.php脚本
<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>
这是因为当<td></td>
条件不符合时if
将不会创建。但当我试图在if
内移动td
时,表格看起来更糟糕
答案 0 :(得分:0)
如果当天没有任何时间,您需要输出空<td></td>
。
<?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