45 lines
1.3 KiB
PHP
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();
|
|
});
|
|
});
|
|
}
|
|
}
|