41 lines
1.2 KiB
PHP
41 lines
1.2 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((new Product())->getTable())->truncate();
|
|
Schema::enableForeignKeyConstraints();
|
|
File::deleteDirectory(\storage_path('app/public/modelfiles/' . (new Product())->getTable()));
|
|
|
|
Range::all()->each(
|
|
fn (Range $range) =>
|
|
Product::factory(\random_int(6, 10))->make()->each(function (Product $product) use ($range) {
|
|
$product->picture = FileStorageHelper::storeFile(
|
|
new Product(),
|
|
new \SplFileInfo(
|
|
\resource_path('../database/factories/assets/products/default-picture-800x800.jpg')
|
|
)
|
|
);
|
|
$product->range()->associate($range);
|
|
$product->save();
|
|
})
|
|
);
|
|
}
|
|
}
|