50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders\Development;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Models\Picto;
|
|
use App\Models\Product;
|
|
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()
|
|
{
|
|
Schema::disableForeignKeyConstraints();
|
|
DB::table((new Product())->getTable())->truncate();
|
|
Schema::enableForeignKeyConstraints();
|
|
|
|
File::deleteDirectory(\storage_path('app/public/modelfiles/' . (new Product())->getTable()));
|
|
|
|
Product::factory(12)->make()->each(function (Product $product) {
|
|
$product->picture = FileStorageHelper::storeFile(
|
|
new Product(),
|
|
new \SplFileInfo(
|
|
\resource_path('../database/factories/assets/products/default-picture-503x503.png')
|
|
)
|
|
);
|
|
$product->picture_thumbnail = FileStorageHelper::storeFile(
|
|
new Product(),
|
|
new \SplFileInfo(
|
|
\resource_path('../database/factories/assets/products/default-picture-310x310.png')
|
|
)
|
|
);
|
|
$product->order = \intval(Product::query()->max('order')) + 1;
|
|
$product->saveQuietly();
|
|
});
|
|
|
|
$pictos = Picto::query()->get();
|
|
Product::query()->each(function (Product $product) use ($pictos) {
|
|
$product->pictos()->saveManyQuietly($pictos->random(\rand(0, 3)));
|
|
});
|
|
}
|
|
}
|