如何从 laravel 中的数组中填写缺失的日期?

How to fill in missing dates from an array in laravel?

我这里有这个功能,可以在我的 table.

中按 created_at 提取数据和分组
private function getSummaryData($seller)
{
    $query = json_decode(json_encode(DB::select(DB::raw("select store_id, 
        DATE_FORMAT(created_at, '%b %Y') ym,
        sum(IF(status='pending', 1, 0)) status_pending,
        sum(IF(status='success', 1, 0)) status_success
        FROM `case_summaries`
        WHERE store_id= " . $seller->store->store_id . "
        GROUP BY DATE_FORMAT(created_at, '%b %Y')
        ORDER BY created_at asc "))), true);

    dd($query);
}

当我尝试使用 dd($query); 时,它会提取某些数据,而我想要的是填写缺失的日期并将值设置为 0 到那些缺失的日期。我怎样才能做到这一点?

dd($query);的结果:

array:24 [
  0 => array:4 [
    "store_id" => 1
    "ym" => "Feb 2018"
    "status_pending" => "0"
    "status_success" => "32"
  ]
  1 => array:4 [
    "store_id" => 1
    "ym" => "Mar 2018"
    "status_pending" => "0"
    "status_success" => "1"
  ]
  2 => array:4 [
    "store_id" => 1
    "ym" => "Apr 2018"
    "status_pending" => "0"
    "status_success" => "5"
  ]
  3 => array:4 [
    "store_id" => 1
    "ym" => "May 2018"
    "status_pending" => "0"
    "status_success" => "6"
  ]
  4 => array:4 [
    "store_id" => 1
    "ym" => "Jun 2018"
    "status_pending" => "0"
    "status_success" => "7"
  ]
  5 => array:4 [
    "store_id" => 1
    "ym" => "Jul 2018"
    "status_pending" => "0"
    "status_success" => "4"
  ]
  6 => array:4 [
    "store_id" => 1
    "ym" => "Aug 2018"
    "status_pending" => "0"
    "status_success" => "6"
  ]
  7 => array:4 [
    "store_id" => 1
    "ym" => "Sep 2018"
    "status_pending" => "0"
    "status_success" => "5"
  ]
  8 => array:4 [
    "store_id" => 1
    "ym" => "Oct 2018"
    "status_pending" => "0"
    "status_success" => "2"
  ]
  9 => array:4 [
    "store_id" => 1
    "ym" => "Nov 2018"
    "status_pending" => "0"
    "status_success" => "12"
  ]
  10 => array:4 [
    "store_id" => 1
    "ym" => "Dec 2018"
    "status_pending" => "0"
    "status_success" => "7"
  ]
  11 => array:4 [
    "store_id" => 1
    "ym" => "Jan 2019"
    "status_pending" => "0"
    "status_success" => "4"
  ]
  12 => array:4 [
    "store_id" => 1
    "ym" => "Feb 2019"
    "status_pending" => "0"
    "status_success" => "6"
  ]
  13 => array:4 [
    "store_id" => 1
    "ym" => "Mar 2019"
    "status_pending" => "0"
    "status_success" => "2"
  ]
  14 => array:4 [
    "store_id" => 1
    "ym" => "Apr 2019"
    "status_pending" => "0"
    "status_success" => "1"
  ]
  15 => array:4 [
    "store_id" => 1
    "ym" => "May 2019"
    "status_pending" => "0"
    "status_success" => "0"
  ]
  16 => array:4 [
    "store_id" => 1
    "ym" => "Jul 2019"
    "status_pending" => "0"
    "status_success" => "1"
  ]
  17 => array:4 [
    "store_id" => 1
    "ym" => "Aug 2019"
    "status_pending" => "1"
    "status_success" => "3"
  ]
  18 => array:4 [
    "store_id" => 1
    "ym" => "Sep 2019"
    "status_pending" => "0"
    "status_success" => "6"
  ]
  19 => array:4 [
    "store_id" => 1
    "ym" => "Oct 2019"
    "status_pending" => "0"
    "status_success" => "3"
  ]
  20 => array:4 [
    "store_id" => 1
    "ym" => "Nov 2019"
    "status_pending" => "0"
    "status_success" => "7"
  ]
  21 => array:4 [
    "store_id" => 1
    "ym" => "Dec 2019"
    "status_pending" => "0"
    "status_success" => "0"
  ]
]

如您所见,array(15)May 2019 跳转到 July 2019。如何在我的数组中添加那些缺失的日期并将 status_pendingstatus_success 的值设置为零?

恐怕没有 SQL 方法可以做到这一点,您必须使用 foreach 来处理它。我想到了这一点,希望对您有所帮助:

$longMonthSeconds = 31 * 24 * 3600;
$time = strtotime($query[0]['ym']);

$data = [];
foreach ($query as $item) {
    if (strtotime($item['ym']) > $time + $longMonthSeconds) {
        $excludedMonth = date('M Y', $time + $longMonthSeconds);
        $data[] = [
            "store_id" => null,
            "ym" => $excludedMonth,
            "status_pending" => "0",
            "status_success" => "0",
        ];
    }
    $data[] = $item;
    $time = strtotime($item['ym']);
}