ldc-gen-game-web/database/seeders/Development/ProductSeeder.php
2026-10-20 12:27:49 +02:00

46 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Database\Seeders\Development;
use App\Lib\Helpers\FileStorageHelper;
use App\Models\Product;
use App\Models\Range;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use SplFileInfo;
class ProductSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
Schema::disableForeignKeyConstraints();
DB::table('products')->truncate();
DB::table('ranges')->truncate();
Schema::enableForeignKeyConstraints();
File::deleteDirectory(\storage_path('app/public/modelfiles/ranges'));
File::deleteDirectory(\storage_path('app/public/modelfiles/products'));
$this->call(RangeSeeder::class);
Range::all()->each(function (Range $range): void {
Product::factory(6)->make()->each(function (Product $product) use ($range): void {
$product->picture = FileStorageHelper::storeFile(
new Range(),
new SplFileInfo(
\resource_path('../database/factories/assets/products/default-picture-550x550.jpg')
)
);
$product->range()->associate($range);
$product->saveQuietly();
});
});
}
}