简体   繁体   中英

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

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

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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