1

I have an admin dashboard that shows a Line Chart with how many users registered each month. I am getting the data from the database and it seems to all work fine but the function is quite large and I wanted to know if there was a more optimal and efficient way to get and return the same data?

I am assigning this function to a variable and passing it to my view.

// Get Monthly Registered Users This year
public function monthlyRegisteredUsers()
{
    $janUsers = User::whereMonth('created_at', 1)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $febUsers = User::whereMonth('created_at', 2)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $marUsers = User::whereMonth('created_at', 3)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $aprUsers = User::whereMonth('created_at', 4)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $mayUsers = User::whereMonth('created_at', 5)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $junUsers = User::whereMonth('created_at', 6)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $julUsers = User::whereMonth('created_at', 7)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $augUsers = User::whereMonth('created_at', 8)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $sepUsers = User::whereMonth('created_at', 9)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $octUsers = User::whereMonth('created_at', 10)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $novUsers = User::whereMonth('created_at', 11)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $decUsers = User::whereMonth('created_at', 12)->whereYear('created_at', Carbon::now()->format('Y'))->count();
    $data  = [$janUsers, $febUsers, $marUsers, $aprUsers, $mayUsers, $junUsers, $julUsers, $augUsers, $sepUsers, $octUsers, $novUsers, $decUsers ];
    return $data;
}

All it does is get the number of registered users each month and assigns it to a variable, then I return the array with the number of users each month. Can this be improved?

Many thanks

1 Answer 1

1

You can use array_map and range to make this code much shorter:

public function monthlyRegisteredUsers()
{
  return array_map(function($month){
    return User::whereMonth('created_at', $month)->whereYear('created_at', Carbon::now()->format('Y'))->count();
  }, range(1,12))
}

And I think you can use a group by to just run one query, let me update my answer by a better query.

public function monthlyRegisteredUsers()
{
  $counts = User::select(DB::raw('MONTH(created_at) month, count(*) as count'))
            ->whereYear('created_at', Carbon::now()->format('Y'))
            ->groupBy(DB::raw('MONTH(created_at)'))
            ->pluck('count', 'month')
            ->toArray();
   return array_map(function($month) use ($counts){
            return Arr::get($counts, $month, 0);
        }, range(1,12));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Reza, thank you so much! It worked perfect! Really appreciate you taking the time to help! Could I adapt this same function to get subscribers per month also?
Yes. Sure. You can add new conditions by where() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.