PHP code example of mohammad-zarifiyan / laravel-chart

1. Go to this page and download the library: Download mohammad-zarifiyan/laravel-chart library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mohammad-zarifiyan / laravel-chart example snippets




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



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');
});

[
    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)
]



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');
});