0

so I have a model named user where all the user data is there. Is it possible to do a graph(?) as a track whenever there are new user register? It can be based on the month or year so it will be easier for me to track the data record. Can anyone give me any hint or guide on this?

  • i guess you just need to make query to fetch data of users, format it based on created_at and use this tute to show chart https://tutorialmeta.com/october-cms/how-to-use-charts-in-octobercms – Hardik Satasiya Jul 03 '20 at 06:43

1 Answers1

0

You need to arrange your data correctly, just follow below code to arrange your data.

// we will show last 7 (6 day + 1 current day) day's stats
$sixDayAgo = \Carbon\Carbon::now()->subDay(6);
$users = RainLab\User\Models\User::whereDate('created_at', '>=', 
                                      $sixDayAgo->toDateString())->get();

$period = \Carbon\CarbonPeriod::create($sixDayAgo, \Carbon\Carbon::now());

$chartData = [];
foreach ($period as $date) {
    $val = ['label' => $date->format('Y-m-d'), 'value' => 0];
    foreach ($users as $user) {
      if($date->format('Y-m-d') == $user->created_at->format('Y-m-d')) {
        $val['value']++;
      }
    }
    $chartData[] = $val;
}
// dd($chartData);

once you get your $chartData then you can show chart

follow https://tutorialmeta.com/october-cms/how-to-use-charts-in-octobercms this tutorial to show $chartData as chart.

Result

enter image description here

if any doubts please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40