亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Laravel 中映射數據

在 Laravel 中映射數據

PHP
慕沐林林 2022-11-04 17:29:52
我無法理解 Laravel 中 API 的響應,這令人困惑。我期待這個輸出{  "month": "January",  "year": 2020,  "history_data": [    {      "id": 27,      "jurnal_id": 12313,      "name": "Transaksi RAHN FLEKSI",      "point": 500,      "status": "SUKSES",      "created_at": "2020-03-18 17:03:26",      "updated_at": "2020-03-18 17:03:26",    },  ]},{  "month": "February",  "year": 2020,  "history_data": [    {      "id": 27,      "jurnal_id": 1231313,      "name": "Transaksi RAHN FLEKSI",      "point": 500,      "status": "SUKSES",      "created_at": "2020-03-18 17:03:26",      "updated_at": "2020-03-18 17:03:26",    }  ],  {    "month": "February",    "year": 2021,    "history_data": [      {        "id": 182,        "jurnal_id": 13213,        "name": "Transaksi RAHN FLEKSI",        "point": 500,        "status": "SUKSES",        "created_at": "2021-02-18 17:03:26",        "updated_at": "2021-02-18 17:03:26",      },      {        "id": 1812313,        "jurnal_id": 12313313,        "name": "Transaksi RAHN FLEKSI",        "point": 500,        "status": "SUKSES",        "created_at": "2021-02-18 17:03:26",        "updated_at": "2021-02-18 17:03:26",      }    ]    {    "month": "March",    "year": 2021,    "history_data": [        {        "id": 183,        "jurnal_id": 132313,        "name": "Transaksi RAHN FLEKSI",        "point": 500,        "status": "SUKSES",        "created_at": "2021-03-18 17:03:26",        "updated_at": "2021-03-18 17:03:26",        }    ]}如果我沒記錯的話,“history_data”按年和月分組。但是,我仍然得到如下回復
查看完整描述

1 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

問題是 :


$makeToCollection = collect($arr)->groupBy('year');

該groupBy方法按給定鍵對集合的項目進行分組


只需使用:


$makeToCollection = collect($arr);

順便說一句,collection 允許你鏈接它的方法來執行流暢的映射和減少:


$historyPoints = $this->historyPoint

    ->select(

        DB::raw("REGEXP_REPLACE(to_char(created_at, 'Month'), '\s+$', '') as month"),

        DB::raw("date_part('Year', created_at) as year")

    )

    ->groupBy('year', 'month')

    ->orderBy('year', 'desc')

    ->get();


/***** Here we go *****/


$makeToCollection = $historyPoints->map(function ($item) {

    $history_data = [];


    foreach (HistoryPointAdd::all() as $data) {

        $month = date('F', strtotime($data->created_at));

        $year  = date('Y', strtotime($data->created_at));


        if ($month == $historyPoint->month && $year == $historyPoint->year) {

            $history_data[] = $data;

        }

    }


    $item['history_data'] = $history_data;


    return $item;

});


return $this->sendSuccess($this->successMessage, $makeToCollection);


查看完整回答
反對 回復 2022-11-04
  • 1 回答
  • 0 關注
  • 183 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號