簡體   English   中英

如何根據日期值在單元格表格刀片 laravel 中顯示數據

[英]How to display data in cell table blade laravel according date value

我需要幫助根據 mysql 數據庫中的日期數據在單元格表刀片中顯示數據。

這是結果

結果

我想要的這個結果

基於日期的數據顯示 header

這是我的 controller:公共 function index_filter(請求 $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::whereRelation('Schedule', 'date', '=', $month1)->with('Schedule')->get();

    // $data_date = DB::table('schedules')
    //              ->leftJoin('employees', 'schedules.employee_id', '=', 'employees.id')
    //              ->select('employees.name','schedules.date')
    //              ->whereBetween('schedules.date', [$month1,$month2])
    //              ->get();

    $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>

如果您對我的問題有任何想法,請以這種方式與我分享,謝謝...

用這個替換第二個@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM