101infotech/data2monthly

此包使用时间戳字段将您的数据转换为按月或按年排序的数据。

v1.2 2022-01-22 10:59 UTC

This package is auto-updated.

Last update: 2024-09-22 16:52:07 UTC


README

使用此包,您将能够使用时间戳字段将您的数据集按月或按年排序,并计算月度/年度支出。您可以使用此包构建一些支出应用程序,并有效地应用于 chart.js。

在您的 composer.json 中添加此包并更新 composer。

composer require 101infotech/data2monthly

代码示例

use Infotech\Data2Monthly\Monthly;

▶ 获取截至当前月份的有序月度数据。

$data = Visitors::all();
Monthly::current($data,'created_at'));

// $data must be a Collection
// 'created_at' exist in collection, holds a timestamp or date

结果:它统计在相应月份找到的数据并返回一个数组。

array:5 [▼
  0 => 0
  1 => 0
  2 => 0
  3 => 0
  4 => 1
]

▶ 获取按月分组字段的总和。

$data = Expenses::all();
Monthly::expenseMonthly($data,'created_at','price');

结果:它对所有 'price' 字段求和并按月分组。

array:12 [▼
  0 => 150
  1 => 900
  2 => 300
  3 => 600
  4 => 300
  5 => 450
  6 => 0
  7 => 0
  8 => 0
  9 => 0
  10 => 0
  11 => 0
]

▶ 获取给定年份截至当前月份的按月统计数据。

$data = Expenses::all();
Monthly::currentWithYear($data,'created_at','price','2021'));

结果:它统计在相应月份找到的数据,直到当前月份,以及相应的年份,并返回一个数组。

array:6 [▼
  0 => 0
  1 => 0
  2 => 0
  3 => 0
  4 => 2
  5 => 0
]

▶ 可用代码的一些列表

Monthly::current($data,'created_at'));
Monthly::monthly($data,'created_at'));
Monthly::yearly($data,'created_at'));
Monthly::expenseCurrent($data,'created_at','price'));
Monthly::expenseMonthly($data,'created_at','price));
Monthly::expenseYearly($data,'created_at','price'));
Monthly::currentWithYear($data,'created_at','price','2021'));
Monthly::monthlyWithYear($data,'created_at','price','2021'));

▶ 在 chart.js 中使用

$dataArr = Monthly::current($data,'created_at'));
<script>
let barGraph = <?=json_encode($dataArr);?>
</script>