简体   繁体   English

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

[英]Laravel : How to display data using for and foreach in blade

I want to display data based on the provided template table, using the for and foreach loop, but when else conditions always display an index of its own我想根据提供的模板表显示数据,使用 for 和 foreach 循环,但是当 else 条件总是显示它自己的索引时

This is my expectation:这是我的期望:

期望值

This is my data:这是我的数据:

数据图像

And this is happening now:现在正在发生这种情况:

结果图片

and this is my code:这是我的代码:

@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

Thanks..谢谢..

Try this, using keyBy function by making day column as index from the result of $scheduleDetails试试这个,使用keyBy function 通过将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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM