mohammad-zarifiyan/laravel-chart 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mohammad-zarifiyan/laravel-chart

最新稳定版本:3.0.1

Composer 安装命令:

composer require mohammad-zarifiyan/laravel-chart

包简介

A Laravel package that helps you to quickly create charts from database.

README 文档

README

This Laravel package helps you to export data for charts using Eloquent ORM easily.

Installation

To install package, just run the following command in the root of your project:

composer require mohammad-zarifiyan/laravel-chart:^3.0

Implementation

First you need to give trait MohammadZarifiyan\LaravelChart\Traits\HasChart to your model. Then use the exportForChart method to extract the data.

  1. The first parameter of this method must be an instance of Carbon\CarbonPeriod that specifies the beginning and end of the total time period.
  2. The second parameter of this method must be a closure that its first parameter is an instance of Illuminate\Database\Eloquent\Builder and its second parameter is an instance of Carbon\CarbonPeriod. In this closure, you must apply conditions to the Illuminate\Database\Eloquent\Builder that limit the data to the time period given by Carbon\CarbonPeriod and then return the desired data for your chart.

The result of exportForChart method is an instance of Illuminate\Support\Collection that includes the data you returned in the closure, so you can use them in your charts.

Example

In the following example, we have calculated the sum of the amount column of the invoices at the end of each day in the period of one week ago until now.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use MohammadZarifiyan\LaravelChart\Traits\HasChart;

class Invoice extends Model
{
    use HasChart;

    protected $fillable = [
        'amount',
    ];

    protected $casts = [
        'amount' => 'integer',
    ];

    public function payment()
        return $this->belongsTo(Payment::class);
    }
}
<?php

use App\Models\Invoice;
use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Builder;

$start = Carbon::now()->subDays(6)->startOfDay();
$end = Carbon::now();
$interval = CarbonInterval::day();
$period = CarbonPeriod::start($start)->setEndDate($end)->setDateInterval($interval);

$output = Invoice::exportForChart($period, function (Builder $builder, CarbonPeriod $period) {
    $builder->whereBetween('created_at', $period);
    
    return $builder->sum('amount');
});

Above code output will be something like this:

[
    2,  // Sum amount, 6 days ago (all day)
    50, // Sum amount, 5 days ago (all day)
    49, // Sum amount, 4 days ago (all day)
    85, // Sum amount, 3 days ago (all day)
    140,// Sum amount, 2 days ago (all day)
    110,// Sum amount, 1 days ago (all day)
    80, // Sum amount, today (till now)
]

You can also filter the table data based on a column in a relation. In the example below, we get the sum of the invoices amount based on their payment time.

<?php

use App\Models\Invoice;
use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Builder;

$start = Carbon::now()->subDays(6)->startOfDay();
$end = Carbon::now();
$interval = CarbonInterval::day();
$period = CarbonPeriod::start($start)->setEndDate($end)->setDateInterval($interval);
    
$output_for_chart = Invoice::exportForChart($period, function (Builder $builder, CarbonPeriod $period) {
    $builder->whereRelation('payment', fn (Builder $builder) => $builder->whereBetween('paid_at', $period));
    
    return $builder->sum('amount');
});

统计信息

  • 总下载量: 41
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-02-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固