HEX
Server: LiteSpeed
System: Linux sv4.hami.host 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64
User: armgroup (1008)
PHP: 8.2.32
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, mail, socket_create, socket_create_listen, socket_create_pair, link, dl, openlog, syslog, stream_socket_server, curl_multi_init
Upload Files
File: //home/armgroup/libs/app/Charts/UsersChart.php
<?php

declare(strict_types=1);

namespace App\Charts;

use App\Models\User;
use Carbon\Carbon;
use Chartisan\PHP\Chartisan;
use ConsoleTVs\Charts\BaseChart;
use Illuminate\Http\Request;

class UsersChart extends BaseChart
{
    public ?array $middlewares = ['auth', 'permission:manage users', 'isActive'];

    private const MONTH = 6;

    /**
     * Handles the HTTP request for the given chart.
     * It must always return an instance of Chartisan
     * and never a string or an array.
     */
    public function handler(Request $request): Chartisan
    {
        $months = $this->getPersianMonths();

        return Chartisan::build()
            ->labels($months)
            ->dataset('تعداد کاربر', $this->getUserCountPerPersianMonth($months));
    }

    private function getPersianMonths(): array
    {
        $months = [];
        for ($i = 0; $i < self::MONTH; $i++) {
            $months[] = jdate(Carbon::now()->subMonths($i))->format('%B');
        }

        return array_reverse($months);
    }

    private function getUserCountPerPersianMonth(array $months): array
    {
        $data = array_fill(0, self::MONTH, 0);
        foreach ($this->getUsers() as $user) {
            $data[array_search(jdate($user->date)->format('%B'), $months)] += $user->count;
        }

        return $data;
    }

    private function getUsers()
    {
        return User::where('created_at', '>', Carbon::now()->subMonths(self::MONTH))
            ->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d') as date, COUNT(*) count")
            ->groupBy('date')
            ->latest()
            ->get();
    }
}