Skip to content
Advertisement

how to do foreach so as not to lose rows and columns in the blade?

how to display the following rows and columns in a foreach? here’s my code

public function index()
{
  $users = User::with('absen')->get();
  $period = CarbonPeriod::create('2020-11-25','2020-11-30');
  
  return view('absen.index',compact('users','period'));
}

–controller–

<div class="box-body">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>tanggal</th>
                        <th>nama</th>
                        <th>masuk</th>
                        <th>keluar</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($period as $date)
                      @foreach ($users as $user)
                          <tr>
                              <td>{{$date->format('Y-m-d')}}</td>
                              <td>{{ $user->nama}}</td>
                              @foreach ($user->absen as $abse)
                                @if ($abse->created_at->format('Y-m-d') == $date->format('Y-m-d'))
                                  <td>{{$abse->masuk}}</td>
                                  <td>{{$abse->keluar}}</td>
                                @endif
                              @endforeach
                          </tr>
                      @endforeach
                    @endforeach
                </tbody>
            </table>
        </div>

–blade–

The following results are missing columns and rows enter image description here

Advertisement

Answer

try this on blade

@forelse ($user->absen as $abse)
     @if ($abse->created_at->format('Y-m-d') == $date->format('Y-m-d'))
          <td>{{$abse->masuk}}</td>
          <td>{{$abse->keluar}}</td>
     @endif
          
@empty
     <td></td>
     <td></td>
@endforelse
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement