Laravel:如何在 Blade 中使用 for 和 foreach 显示数据

我想使用 for 和 foreach 循环根据提供的模板表显示数据,但是当 else 条件始终显示其自己的索引时

这是我的期望:

https://img4.mukewang.com/65041dae00011d0509830122.jpg

这是我的数据:

https://img2.mukewang.com/65041db600018f6402020226.jpg

这现在正在发生:

https://img2.mukewang.com/65041dbf000183ce06510085.jpg

这是我的代码:


@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

谢谢..


当年话下
浏览 77回答 1
1回答

慕莱坞森

尝试一下,使用keyBy函数将day列作为结果的索引$scheduleDetails@php $newScheduleDetails = $scheduleDetails->keyBy('day'); @endphp@for ($i = 0; $i < 7; $i++)&nbsp; &nbsp;@if($newScheduleDetails->has($i))&nbsp; &nbsp; &nbsp; &nbsp;<td class="p-1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="from[]" value="{{$newScheduleDetails->get($i)->from_time }}" id="from{{$i}}" style="width:50px;margin:auto" type="text">&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="p-1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="until[]" value="{{$newScheduleDetails->get($i)->until_time }}" id="until{{$i}}" style="width:50px;margin:auto" type="text">&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp;@else&nbsp; &nbsp; &nbsp; &nbsp; <td>{{$i}}</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>{{$i}}</td>&nbsp; &nbsp;@endif@endfor
打开App,查看更多内容
随时随地看视频慕课网APP