Fenz
Fenz

Reputation: 139

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 :

expectation img

This is my data :

data img

And this is happening now :

result img

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..

Upvotes: 0

Views: 1226

Answers (1)

Ariel Pepito
Ariel Pepito

Reputation: 659

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

Upvotes: 1

Related Questions