Missing seasons added
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use App\Models\Season;
|
||||
use App\Models\Player;
|
||||
use App\Models\Club;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('seasons', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('title_ru');
|
||||
$table->string('title_en');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
// test seed
|
||||
for ($i = 2020; $i < 2026; $i++) {
|
||||
Season::create([
|
||||
'title_ru' => $i . ' год',
|
||||
'title_en' => $i . ' year',
|
||||
]);
|
||||
}
|
||||
|
||||
Schema::create('player_season', function (Blueprint $table) {
|
||||
$table->id();
|
||||
|
||||
$table->foreignIdFor(Season::class)
|
||||
->constrained()
|
||||
->onUpdate('cascade')
|
||||
->onDelete('restrict');
|
||||
|
||||
$table->foreignIdFor(Player::class)
|
||||
->constrained()
|
||||
->onUpdate('cascade')
|
||||
->onDelete('restrict');
|
||||
|
||||
$table->unique(['season_id', 'player_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('player_season');
|
||||
Schema::dropIfExists('seasons');
|
||||
}
|
||||
};
|
||||
@@ -14,7 +14,7 @@ class ClubSeeder extends Seeder
|
||||
public function run(): void
|
||||
{
|
||||
Club::factory()
|
||||
->count(12)
|
||||
->count(10)
|
||||
->create();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
@@ -15,6 +14,14 @@ class DatabaseSeeder extends Seeder
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
//
|
||||
$this->command->info('Starting database seeding...');
|
||||
|
||||
$this->call([
|
||||
ClubSeeder::class,
|
||||
PlayerSeeder::class,
|
||||
SeasonPlayerSeeder::class,
|
||||
]);
|
||||
|
||||
$this->command->info('Database seeding finished successfully.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,19 +2,60 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Club;
|
||||
use App\Models\Player;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Database\Seeders\Traits\HasPlayerNames;
|
||||
|
||||
class PlayerSeeder extends Seeder
|
||||
{
|
||||
use HasPlayerNames;
|
||||
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Player::factory()
|
||||
->count(30)
|
||||
->create();
|
||||
$this->command->info('Seeding players for each club with unique squad numbers and names...');
|
||||
|
||||
$clubs = Club::all();
|
||||
$playerNames = $this->getPlayerNames();
|
||||
$totalNames = count($playerNames);
|
||||
$nameIndex = 0;
|
||||
|
||||
if ($clubs->isEmpty()) {
|
||||
$this->command->warn('No clubs found. Skipping player seeding.');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->command->getOutput()->progressStart($clubs->count());
|
||||
|
||||
foreach ($clubs as $club) {
|
||||
$availableNumbers = range(1, 99);
|
||||
shuffle($availableNumbers);
|
||||
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$squadNumber = array_pop($availableNumbers);
|
||||
if (is_null($squadNumber)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$name = $playerNames[$nameIndex % $totalNames];
|
||||
$nameIndex++;
|
||||
|
||||
Player::create([
|
||||
'club_id' => $club->id,
|
||||
'squad_number' => $squadNumber,
|
||||
'full_name_ru' => $name['ru'],
|
||||
'full_name_en' => $name['en'],
|
||||
'weight' => rand(65, 105),
|
||||
'height' => rand(160, 210),
|
||||
]);
|
||||
}
|
||||
$this->command->getOutput()->progressAdvance();
|
||||
}
|
||||
|
||||
$this->command->getOutput()->progressFinish();
|
||||
$this->command->info("\nFinished seeding players.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.');
|
||||
}
|
||||
}
|
||||
+4
-43
@@ -1,25 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
namespace Database\Seeders\Traits;
|
||||
|
||||
use App\Models\Player;
|
||||
use App\Models\Club;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* @extends Factory<Player>
|
||||
*/
|
||||
class PlayerFactory extends Factory
|
||||
trait HasPlayerNames
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
protected function getPlayerNames(): array
|
||||
{
|
||||
$names = [
|
||||
return [
|
||||
['ru' => 'Александр Овечкин', 'en' => 'Alexander Ovechkin'],
|
||||
['ru' => 'Евгений Малкин', 'en' => 'Evgeni Malkin'],
|
||||
['ru' => 'Никита Кучеров', 'en' => 'Nikita Kucherov'],
|
||||
@@ -121,31 +108,5 @@ class PlayerFactory extends Factory
|
||||
['ru' => 'Анатолий Семенов', 'en' => 'Anatoly Semenov'],
|
||||
['ru' => 'Андрей Ломакин', 'en' => 'Andrei Lomakin'],
|
||||
];
|
||||
|
||||
$name = $names[array_rand($names)];
|
||||
$club = Club::inRandomOrder()->first();
|
||||
|
||||
if (!$club) {
|
||||
throw new Exception('No clubs found in the database. Please seed the clubs table first.');
|
||||
}
|
||||
|
||||
$usedNumbers = Player::where('club_id', $club->id)->pluck('squad_number')->toArray();
|
||||
|
||||
$availableNumbers = array_diff(range(1, 99), $usedNumbers);
|
||||
|
||||
if (empty($availableNumbers)) {
|
||||
throw new Exception("No available squad numbers for club ID: {$club->id}.");
|
||||
}
|
||||
|
||||
$squadNumber = $availableNumbers[array_rand($availableNumbers)];
|
||||
|
||||
return [
|
||||
'full_name_ru' => $name['ru'],
|
||||
'full_name_en' => $name['en'],
|
||||
'weight' => rand(65, 105),
|
||||
'height' => rand(160, 210),
|
||||
'squad_number' => $squadNumber,
|
||||
'club_id' => $club->id,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user