如何做 foreach 才不会丢失 blade 中的行和列?
how to do foreach so as not to lose rows and columns in the blade?
如何在foreach中显示下面的行和列?
这是我的代码
public function index()
{
$users = User::with('absen')->get();
$period = CarbonPeriod::create('2020-11-25','2020-11-30');
return view('absen.index',compact('users','period'));
}
--控制器--
<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--
以下结果缺少列和行
enter image description here
在 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
如何在foreach中显示下面的行和列? 这是我的代码
public function index()
{
$users = User::with('absen')->get();
$period = CarbonPeriod::create('2020-11-25','2020-11-30');
return view('absen.index',compact('users','period'));
}
--控制器--
<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--
以下结果缺少列和行 enter image description here
在 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