【发布时间】:2022-01-18 06:08:34
【问题描述】:
我需要帮助根据 MySQL 数据库中的日期数据在单元格表格刀片中显示数据。如果您对我的问题有任何想法,请分享。
结果
预期结果
控制器
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
]);
}
刀片/视图
<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>
【问题讨论】: