【问题标题】:How to display data in cell table blade Laravel according date value如何根据日期值在单元格表刀片 Laravel 中显示数据
【发布时间】: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>

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    用这个替换第二个@foreach

    @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
    

    【讨论】:

    • 还是一样的先生,我试过用 carbon::parse($items->date)->format('d') == '01' to '30'日期有超过2个数据,下一个日期将显示在下一个表格中
    • 你的意思是下一行 /
    • 不,先生,我将 if 设置为 if 条件 else .
    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2019-02-22
    • 2021-01-20
    • 2023-01-08
    • 2020-01-27
    相关资源
    最近更新 更多