Laravel : 如何在 blade 中使用 for 和 foreach 显示数据
Laravel : How to display data using for and foreach in blade
我想根据提供的模板 table 显示数据,使用 for 和 foreach 循环,但是 when else 条件总是显示它自己的索引
这是我的期望:
这是我的数据:
这正在发生:
这是我的代码:
@for ($i = 0; $i < 7; $i++)
@foreach ($scheduleDetails as $item)
@if ($i == $item->day)
<td class="p-1">
<input name="from[]" value="{{substr($item->from_time,0,-3)}}" id="from{{$i}}" style="width:50px;margin:auto" class="text-center" type="text" readonly>
</td>
<td class="p-1">
<input name="until[]" value="{{substr($item->until_time,0,-3)}}" id="until{{$i}}" style="width:50px;margin:auto" class="text-center" type="text" readonly>
</td>
@else
<td>{{$i}}</td>
@endif
@endforeach
@endfor
谢谢..
试试这个,使用 keyBy
函数,将 day
列作为 $scheduleDetails
结果的索引
@php $newScheduleDetails = $scheduleDetails->keyBy('day'); @endphp
@for ($i = 0; $i < 7; $i++)
@if($newScheduleDetails->has($i))
<td class="p-1">
<input name="from[]" value="{{$newScheduleDetails->get($i)->from_time }}" id="from{{$i}}" style="width:50px;margin:auto" type="text">
</td>
<td class="p-1">
<input name="until[]" value="{{$newScheduleDetails->get($i)->until_time }}" id="until{{$i}}" style="width:50px;margin:auto" type="text">
</td>
@else
<td>{{$i}}</td>
<td>{{$i}}</td>
@endif
@endfor
我想根据提供的模板 table 显示数据,使用 for 和 foreach 循环,但是 when else 条件总是显示它自己的索引
这是我的期望:
这是我的数据:
这正在发生:
这是我的代码:
@for ($i = 0; $i < 7; $i++)
@foreach ($scheduleDetails as $item)
@if ($i == $item->day)
<td class="p-1">
<input name="from[]" value="{{substr($item->from_time,0,-3)}}" id="from{{$i}}" style="width:50px;margin:auto" class="text-center" type="text" readonly>
</td>
<td class="p-1">
<input name="until[]" value="{{substr($item->until_time,0,-3)}}" id="until{{$i}}" style="width:50px;margin:auto" class="text-center" type="text" readonly>
</td>
@else
<td>{{$i}}</td>
@endif
@endforeach
@endfor
谢谢..
试试这个,使用 keyBy
函数,将 day
列作为 $scheduleDetails
@php $newScheduleDetails = $scheduleDetails->keyBy('day'); @endphp
@for ($i = 0; $i < 7; $i++)
@if($newScheduleDetails->has($i))
<td class="p-1">
<input name="from[]" value="{{$newScheduleDetails->get($i)->from_time }}" id="from{{$i}}" style="width:50px;margin:auto" type="text">
</td>
<td class="p-1">
<input name="until[]" value="{{$newScheduleDetails->get($i)->until_time }}" id="until{{$i}}" style="width:50px;margin:auto" type="text">
</td>
@else
<td>{{$i}}</td>
<td>{{$i}}</td>
@endif
@endfor