format('Y'); // $currentYear = 2023; $currentMonth = now()->format('m'); // $currentMonth = 4; $users = User::all(); foreach ($users as $user) { $userId = $user->id; $user_list = LeaderboardMaster::orderBy('total_score', 'desc')->get(); $position = null; foreach ($user_list as $index => $user){ if($user->user_id == $userId){ $position = $index + 1; break; } } if (isset($position)) { $monthDate = Carbon::createFromDate($currentYear, $currentMonth, 1)->format('Y-m'); MonthlyPosition::updateOrCreate([ 'user_id' => $userId, 'month' => $monthDate, ], [ 'position' => $position, ]); } } $this->info('Users Position Stored Successfully!'); // return Command::SUCCESS; } }