Reputation: 805
I want the output to be like this
The problem is 4th row doesn't have a value but the value is showing in that..
My code looks like this
@if($fridges)
@foreach($fridges as $index => $fridge)
@foreach($fridgereadings as $index1 => $fridgereading)
@if($fridge->fridge_no == $fridgereading->fridge_id)
@php $morning_reading = $fridgereading->morning_reading; @endphp
@endif
@endforeach
<tr>
<td>{{ $fridge->fridge_no }}</td>
<input id="fridge_id" name="fridge_id[]" value="{{ $fridge->fridge_no }}" type="hidden" class="form-control">
<td><input value="@if(!empty($morning_reading)) {{ $morning_reading }} @endif" id="morning_reading" name="morning_reading[]" type="text" class="form-control"></td>
<td><input id="evening_reading" name="evening_reading[]" type="text" class="form-control"></td>
</tr>
@endforeach
@endif
The fourth row Fridge No
doesn't have a value in the Morning reading
Column but it's showing how can we prevent that..
Upvotes: 0
Views: 67
Reputation: 147166
Your issue is that you're not resetting the value of $morning_reading
on each pass through the loop, so when you process fridge 4 and find no value for it in $fridgereadings
, $morning_reading
retains its previous value (in this case, 30
). You need to assign a default value in case there is no reading:
@if($fridges)
@foreach($fridges as $index => $fridge)
@php $morning_reading = ''; @endphp
@foreach($fridgereadings as $index1 => $fridgereading)
@if($fridge->fridge_no == $fridgereading->fridge_id)
@php $morning_reading = $fridgereading->morning_reading; @endphp
@endif
@endforeach
<tr>
<td>{{ $fridge->fridge_no }}</td>
<input id="fridge_id" name="fridge_id[]" value="{{ $fridge->fridge_no }}" type="hidden" class="form-control">
<td><input value="@if(!empty($morning_reading)) {{ $morning_reading }} @endif" id="morning_reading" name="morning_reading[]" type="text" class="form-control"></td>
<td><input id="evening_reading" name="evening_reading[]" type="text" class="form-control"></td>
</tr>
@endforeach
@endif
Upvotes: 2