首页 > 解决方案 > 在 Laravel 中映射数据

问题描述

我无法理解 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”按年和月分组。但是,我仍然得到如下回复

"2021": [
        {
            "month": "February",
            "year": "2021",
            "history_data": {
                "id": 2,
                "id_jurnal": 1846756745650,
                "name": "Transaksi ARRUM SAFAR",
                "point": 1200,
                "status": "SUKSES",
                "created_at": "2021-02-13 10:11:21",
                "updated_at": "2020-02-13 10:11:21",
                "cif_number": "1003002713"
            }
        },
        {
            "month": "March",
            "year": "2021",
            "history_data": {
                "id": 29,
                "id_jurnal": 1015205749121113,
                "name": "Transaksi RAHN FLEKSI",
                "point": 500,
                "status": "SUKSES",
                "created_at": "2021-03-18 17:26:57",
                "updated_at": "2020-03-18 17:26:57",
                "cif_number": "1015205749"
            }
        }
    ],
    "2020": [
        {
            "month": "February",
            "year": "2020",
            "history_data": {
                "id": 1,
                "id_jurnal": 1846756745652,
                "name": "Transaksi ARRUM SAFAR",
                "point": 1200,
                "status": "SUKSES",
                "created_at": "2020-02-13 09:52:56",
                "updated_at": "2020-02-13 09:52:56",
                "cif_number": "1003002713"
            }
        },
  ]

这是 PHP / Laravel 代码

$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();


            $arr = [];
            foreach($historyPoints as $historyPoint) {
                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) {
                        $arr[] = [
                            'month' => $historyPoint->month,
                            'year' => $historyPoint->year,
                            'history_data' => $data 
                        ];
                    }
                }
            }
            $makeToCollection = collect($arr)->groupBy('year');

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

我正在使用 PostgreSQL 作为数据库。非常感谢

标签: phplaravelpostgresqlapi

解决方案


问题是 :

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

推荐阅读