1

can someone help me please.. i have database with activities members and want to display in chartjs last 7 days data.

id member_id bonus created_at
1    1         3     2020-04-16
2    1         3     2020-04-17
3    1         7     2020-04-18
4    1         5     2020-04-19
5    1         15    2020-04-20
6    1         10    2020-04-21
7    1         9     2020-04-22 
8    1         11    2020-04-24

my laravel code

$chart = charts::where('member_id', Auth::user()->id)->orderBy('id', 'asc')->where('created_at', '>=', \Carbon\Carbon::today()->subDays(7))->get();

$bonus = [];
foreach ($charts as $index => $count) {
            $bonus[] = [$count->bonus];
        }
return view('index', ['bonus' => $bonus]);

problem is in chart because display data from 2020-04-17 to 2020-04-24 and it's logic, but i want to show also in chart days when member don't have any bonus for example 2020-04-23 not exists because member didn't have bonus this day, so i need to show in chart 0 in 2020-04-23.

can someone help me ?

John Jack
  • 13
  • 6
  • can you please check this answer; https://stackoverflow.com/a/61323748/2188922 – Ersoy Apr 24 '20 at 01:56
  • thanks, but my case is not similar to this answer. – John Jack Apr 24 '20 at 09:34
  • @Ersoy can i ask my last question please if i have another column field in table like "bonus" for example another field named "type" and i want to include it with dates created_at in query in get() and pluck the same you did for "bonus" please how i can do that ? thanks so much my friend for help... – John Jack Apr 25 '20 at 15:33
  • put `type` in `get`, `$activities->pluck('bonus', 'date')` and `$activities->pluck('type', 'date')`. the answer is locked - this is my final answer because i made a real-time development in comments. hope you understand. good luck – Ersoy Apr 25 '20 at 15:40
  • how please do i need to have two get and pluck in my query ? please if u can post final query please!! – John Jack Apr 26 '20 at 12:20

1 Answers1

1

I assumed your created_at column is timestamp (formatting added for that case)

public function index()
{
    $start = Carbon::today()->subDays(7);
    $end = Carbon::yesterday();

    $activities = Activity::where('member_id', Auth::user()->id)
        ->where('created_at', '>=', Carbon::today()->subDays(7))
        ->get(['bonus', DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d') as date")])
        ->pluck('bonus', 'date');

    $dates = $this->generateDates($start, $end); // you fill zero valued dates

    return $dates->merge($activities); // overwrite your bonuses with the zero values
}

public function generateDates(Carbon $startDate, Carbon $endDate, $format = 'Y-m-d')
{
    $dates = collect();
    $startDate = $startDate->copy();

    for ($date = $startDate; $date->lte($endDate); $date->addDay()) {
        $dates->put($date->format($format), 0);
    }

    return $dates;
}

to format dates

$formattedDates = $dates->merge($activities)
            ->keys()
            ->transform(function ($date) {
                return Carbon::parse($date)->format('d F');
            });
Ersoy
  • 8,816
  • 6
  • 34
  • 48
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/212463/discussion-on-answer-by-ersoy-laravel-chartjs-dates-skipped). – Samuel Liew Apr 25 '20 at 03:06