pluck('user_id') ->toArray(); // Aktive Berater mit den meisten direkten Partnern holen $consultants = User::where('active', 1) ->whereNotNull('m_sponsor') ->whereNotIn('id', $existing_user_ids) ->select('users.*') ->selectSub( DB::table('users as u2')->selectRaw('COUNT(*)')->whereColumn('u2.m_sponsor', 'users.id'), 'direct_partners_count' ) ->orderByDesc('direct_partners_count') ->limit($limit) ->get(); $count = 0; foreach ($consultants as $consultant) { IncentiveParticipant::create([ 'incentive_id' => $incentive_id, 'user_id' => $consultant->id, 'accepted_terms_at' => Carbon::now(), ]); $count++; } $this->command->info("Incentive #{$incentive_id}: {$count} Teilnehmer hinzugefuegt (bestehende uebersprungen)."); } }