isEmpty()) { $this->call(FeedSeeder::class); $feeds = Feed::all(); } foreach ($times as $time) { $schedule = FeedSchedule::firstOrCreate([ 'scheduled_at' => $time, ]); // Attach 1-2 random feeds to each schedule $selectedFeeds = $feeds->random(rand(1, 2)); foreach ($selectedFeeds as $feed) { // Check if already attached to avoid duplicates if re-run if (! $schedule->feeds()->where('feed_id', $feed->id)->exists()) { $schedule->feeds()->attach($feed->id, [ 'quantity' => rand(10, 50), ]); } } } } }