簡體   English   中英

將垂直表轉換/映射為水平表

[英]Converting/Mapping vertical table to horizontal

我無法從數據庫映射 object 以顯示在我的表中。 我的數據庫中有一個這樣的表。

name    total   date
Awoc1   100   9/14/2022
Awoc1   200   9/15/2022
Awoc1   300   9/16/2022
Awoc2   100   9/14/2022
Awoc2   200   9/15/2022
Awoc2   300   9/16/2022
Awoc3   100   9/14/2022
Awoc3   200   9/15/2022
Awoc3   300   9/16/2022

我將數據作為這樣的對象獲取,這就是我得到的響應。

[
{ "total": "300", "date": "2022-09-14", "name": "AWOC1"},
{ "total": "200", "date": "2022-09-14", "name": "AWOC2"},
{ "total": "100", "date": "2022-09-14", "name": "AWOC3"},
{ "total": "300", "date": "2022-09-15", "name": "AWOC1"},
{ "total": "200", "date": "2022-09-15", "name": "AWOC2"},
{ "total": "100", "date": "2022-09-15", "name": "AWOC3"},
{ "total": "300", "date": "2022-09-16", "name": "AWOC1"},
{ "total": "200", "date": "2022-09-16", "name": "AWOC2"},
{ "total": "100", "date": "2022-09-16", "name": "AWOC3"},   
]

我希望做的是在表格中顯示這樣的對象。 一種水平的。

        9/14/2022   9/15/2022   9/16/2022
Awoc1   100         200         300
Awoc2   100         200         300
Awoc3   100         200         300

如果我是你,我會創建兩個變量。 第一個變量是唯一日期列表。 第二個是按namedate列分組的數據。

$data = json_decode('[
    { "total": "300", "date": "2022-09-14", "name": "AWOC1"},
    { "total": "200", "date": "2022-09-14", "name": "AWOC2"},
    { "total": "100", "date": "2022-09-14", "name": "AWOC3"},
    { "total": "300", "date": "2022-09-15", "name": "AWOC1"},
    { "total": "200", "date": "2022-09-15", "name": "AWOC2"},
    { "total": "100", "date": "2022-09-15", "name": "AWOC3"},
    { "total": "300", "date": "2022-09-16", "name": "AWOC1"},
    { "total": "200", "date": "2022-09-16", "name": "AWOC2"},
    { "total": "100", "date": "2022-09-16", "name": "AWOC3"}
]');

$data = collect($data);

$dates = $data->pluck('date')->unique();

//        $dates Output:
//
//        array:3 [▼
//          0 => "2022-09-14"
//          3 => "2022-09-15"
//          6 => "2022-09-16"
//        ]

$transformedData = $data->groupBy('name')
    ->map(function ($item) {
        return $item->groupBy('date')->flatten()->pluck('total', 'date');
    });

//        $transformedData Output:
//
//        array:3 [▼
//          "AWOC1" => array:3 [▼
//            "2022-09-14" => "300"
//            "2022-09-15" => "300"
//            "2022-09-16" => "300"
//          ]
//          "AWOC2" => array:3 [▼
//            "2022-09-14" => "200"
//            "2022-09-15" => "200"
//            "2022-09-16" => "200"
//          ]
//          "AWOC3" => array:3 [▼
//            "2022-09-14" => "100"
//            "2022-09-15" => "100"
//            "2022-09-16" => "100"
//          ]
//        ]

return view('test', ['transformedData' => $transformedData, 'dates' => $dates]);

test.blade.php:(將其視為偽代碼,您可以輕松地將其轉換為 Vue、React 或 Angular)

<table>
    <thead>
        <tr>
            <th></th>
            @foreach($dates as $date)
                <th>{{ $date }}</th>
            @endforeach
        </tr>
    </thead>
    @foreach($transformedData as $name => $totals)
        <tr>
            <td>{{ $name }}</td>
            @foreach($totals as $total)
                <td>{{ $total }}</td>
            @endforeach
        </tr>
    @endforeach
</table>

暫無
暫無

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

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