subMonth(); $newUser = User::whereMonth('created_at', $now->month) ->whereYear('created_at', $now->year) ->count(); $newUserLastMonth = User::whereMonth('created_at', $lastMonth->month) ->whereYear('created_at', $lastMonth->year) ->count(); if ($newUserLastMonth > 0) { $growthRate = ($newUser - $newUserLastMonth) / $newUserLastMonth; $growthRate = $growthRate * 100; $growthRate = number_format($growthRate, 2, '.', ''); if ($growthRate > 0) { $growthRate = "+{$growthRate}%"; } else { $growthRate = "{$growthRate}%"; } } else { $growthRate = '+100%'; } return [ 'new_users' => $newUser, 'growth_rate' => $growthRate, ]; } }