ldc-showroom-bo/database/seeders/Development/RangeSeeder.php

58 lines
1.8 KiB
PHP
Raw Permalink Normal View History

<?php
2024-01-05 17:36:16 +01:00
namespace Database\Seeders\Development;
use App\Models\Brand;
use App\Models\Font;
use App\Models\Range;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Uwil\LaravelCommon\Helpers\FileHelper;
class RangeSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run(): void
{
Schema::disableForeignKeyConstraints();
DB::table((new Range())->getTable())->truncate();
Schema::enableForeignKeyConstraints();
File::deleteDirectory(\storage_path('app/public/modelfiles/' . (new Range())->getTable()));
$fonts = Font::all();
Brand::all()->each(
fn(Brand $brand) =>
Range::factory(\random_int(6, 16))->make()->each(function (Range $range) use ($brand, $fonts) {
2024-01-15 16:32:49 +01:00
if ($range->width === 1) {
$range->picture = FileHelper::storeFile(
2024-01-15 16:32:49 +01:00
new Range(),
new \SplFileInfo(
\resource_path('../database/factories/assets/ranges/default-picture-600x500.jpg')
),
prefix: $range->tenant_id
2024-01-15 16:32:49 +01:00
);
} else {
$range->picture = FileHelper::storeFile(
2024-01-15 16:32:49 +01:00
new Range(),
new \SplFileInfo(
\resource_path('../database/factories/assets/ranges/default-picture-1300x500.jpg')
),
prefix: $range->tenant_id
2024-01-15 16:32:49 +01:00
);
}
$range->brand()->associate($brand);
$range->font()->associate($fonts->random(1)->first());
$range->save();
})
);
}
}