Missing seasons added
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\Season;
|
||||
use App\Models\Player;
|
||||
|
||||
class SeasonPlayerSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$this->command->info('Seeding season-player relationships...');
|
||||
|
||||
$seasons = Season::all();
|
||||
$allPlayerIds = Player::pluck('id');
|
||||
|
||||
if ($allPlayerIds->isEmpty()) {
|
||||
$this->command->warn('No players found. Skipping season-player seeding.');
|
||||
return;
|
||||
}
|
||||
|
||||
$playersToAttachCount = min(5, $allPlayerIds->count());
|
||||
|
||||
foreach ($seasons as $season) {
|
||||
$randomPlayerIds = $allPlayerIds->random($playersToAttachCount);
|
||||
|
||||
// syncWithoutDetaching гарантирует уникальность связи.
|
||||
// Если такая пара (season_id, player_id) уже есть, она не будет добавлена.
|
||||
$season->players()->syncWithoutDetaching($randomPlayerIds);
|
||||
}
|
||||
|
||||
$this->command->info('Finished seeding season-player relationships.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user