php - How to display data in cell table blade Laravel according date value
I need help to display data in the cell table blade according to date data in the MySQL database. If you have any ideas for my problem, please share.
Result
Expected Result
Controller
public function index_filter(Request $request)
{
$month1 = $request->month1;
$month2 = $request->month2;
$dt = Schedule::where('date', $month1)->value('date');
$dtx = Schedule::where('employee_id', 1)->get();
$dtt = Carbon::parse($dt)->daysInMonth;
$month_label = Carbon::parse($dt)->format('M-Y');
$data_date = Employee::with([
'schedule' => function ($q) use ($month1, $month2) {
$q->whereBetween('date', [$month1, $month2]);
}
])->get();
$name = DB::table('employees')->get();
return view('admin.filter-laporan', [
'dt' => $dt,
'dtt' => $dtt,
'dtx' => $dtx,
'data_date' => $data_date,
'month_label' => $month_label,
'month1' => $month1,
'month2' => $month2,
'name' => $name
]);
}
Blade/View
<table class="table table-striped table-bordered">
<thead>
<tr>
<th rowspan="2" style="text-align: center;">No</th>
<th rowspan="2" style="text-align: center;">Nama</th>
<th colspan="{{ $dtt }}" style="text-align: center;">{{ $month_label }}</th>
</tr>
<tr>
@php
$number = 1;
@endphp
@for ($i = 0; $i < $dtt; $i++)
<th>{{ $number++ }}</th>
@endfor
</tr>
</thead>
<tbody>
@foreach ($data_date as $key => $item)
<tr>
<td>{{ ++$key }}</td>
<td>{{$item->name}}</td>
@foreach ($item->schedule as $items)
@if ($items->status == 'D')
<td style="background-color: green;">{{$items->date }}</td>
@endif
@if ($items->status == 'C')
<td style="background-color: yellow;">X</td>
@endif
@if ($items->status == 'LD')
<td style="background-color: red;">X</td>
@endif
@if ($items->status == '')
<td>-</td>
@endif
@endforeach
</tr>
@endforeach
</tbody>
</table>
Answer
Solution:
Replace the 2nd @foreach
with this one
@for ($i = 1; $i <= $dtt; $i++)
@foreach ($item->schedule as $items)
@if(date('d', strtotime($items->date)) == $i)
@if ($items->status == 'D')
<td style="background-color: green;">{{ $items->date }}</td>
@endif
@if ($items->status == 'C')
<td style="background-color: yellow;">X</td>
@endif
@if ($items->status == 'LD')
<td style="background-color: red;">X</td>
@endif
@if ($items->status == '')
<td>-</td>
@endif
@endif
@endforeach
@endfor
Source