1
0
Fork 0
BitHarbor/backend/database/seeders/DatabaseSeeder.php
2026-04-29 15:35:06 +02:00

56 lines
1.5 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Product;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
use WithoutModelEvents;
/**
* Seed the application's database.
*/
public function run(): void
{
// User::factory(10)->create();
User::factory()->create([
'name' => 'Test User',
'email' => 'test@example.com',
]);
$brands = Brand::factory(12)->create();
$this->call(CategorySeeder::class);
$allCategories = Category::query()->get();
$parentCategories = $allCategories->whereNull('parent_id')->values();
$childCategories = $allCategories->whereNotNull('parent_id')->values();
if ($allCategories->isEmpty()) {
return;
}
Product::factory(100)
->state(fn (): array => [
'category_id' => $childCategories->isNotEmpty()
? $childCategories->random()->id
: $allCategories->random()->id,
'brand_id' => $brands->random()->id,
])
->create();
Product::factory(30)
->state(fn (): array => [
'category_id' => $parentCategories->random()->id,
'brand_id' => $brands->random()->id,
])
->create();
}
}