簡體   English   中英

Laravel:如何在刀片中使用 for 和 foreach 顯示數據

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

我想根據提供的模板表顯示數據,使用 for 和 foreach 循環,但是當 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 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