ldc-gen-game-web/database/seeders/Development/ProductSeeder.php
Jérémy Munsch d803889960
Sync 9 FAQ ok
2023-11-30 11:10:02 +01:00

45 lines
1.3 KiB
PHP

<?php
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;
class ProductSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
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) {
Product::factory(6)->make()->each(function (Product $product) use ($range) {
$product->picture = FileStorageHelper::storeFile(
new Range(),
new \SplFileInfo(
\resource_path('../database/factories/assets/products/default-picture-550x550.jpg')
)
);
$product->range()->associate($range);
$product->saveQuietly();
});
});
}
}