70 lines
2.5 KiB
PHP
70 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders\Development;
|
|
|
|
use App\Models\Meat;
|
|
use App\Models\Product;
|
|
use App\Models\Range;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ProductSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run(): void
|
|
{
|
|
Product::factory(25)->create();
|
|
|
|
$productModels = Product::all();
|
|
foreach ($productModels as $productModel) {
|
|
$rangeModel = Range::inRandomOrder()->first();
|
|
// Morph relationships (Range/Subrange).
|
|
DB::table('productables')->insert(
|
|
[
|
|
'product_id' => $productModel->id,
|
|
'productable_id' => $rangeModel->id,
|
|
'productable_type' => 'App\Models\Range'
|
|
]
|
|
);
|
|
if (\fake()->boolean(75)) {
|
|
/** @var \Illuminate\Database\Eloquent\Collection<int, \App\Models\Subrange> $subrangeModels */
|
|
$subrangeModels = $rangeModel->subranges;
|
|
DB::table('productables')->insert(
|
|
[
|
|
'product_id' => $productModel->id,
|
|
'productable_id' => $subrangeModels->random()->id,
|
|
'productable_type' => 'App\Models\Subrange'
|
|
]
|
|
);
|
|
}
|
|
|
|
if (\fake()->boolean(75)) {
|
|
$meatModel = Meat::inRandomOrder()->first();
|
|
// Morph relationships (Meat/Submeat).
|
|
DB::table('productables')->insert(
|
|
[
|
|
'product_id' => $productModel->id,
|
|
'productable_id' => $meatModel->id,
|
|
'productable_type' => 'App\Models\Meat'
|
|
]
|
|
);
|
|
// @phpstan-ignore-next-line
|
|
if (\fake()->boolean(75)) {
|
|
/** @var \Illuminate\Database\Eloquent\Collection<int, \App\Models\Submeat> $submeatModels */
|
|
$submeatModels = $meatModel->submeats;
|
|
DB::table('productables')->insert(
|
|
[
|
|
'product_id' => $productModel->id,
|
|
'productable_id' => $submeatModels->random()->id,
|
|
'productable_type' => 'App\Models\Submeat'
|
|
]
|
|
);
|
|
}
|
|
} //end if
|
|
} //end foreach
|
|
}
|
|
}
|