Chartjs-Get the count of dayname Failed โ€“ Laravel 9

0๐Ÿ‘

โœ…

You can use array reduce() function for this purpose. It is a php array function that you can find docs in here and also for laravel collection

$dayCounts = DB::table('watchlists')
      ->select('created_at')
      ->get()
      ->reduce(function ($car, $item) {
          $day       = Carbon::parse($item->created_at)->format('l');
          $car[$day] = ($car[$day] ?? 0) + 1;
          return $car;
      });

Leave a comment