38 lines
1.1 KiB
PHP
38 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\IncoNutritionalValue;
|
|
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()
|
|
{
|
|
DB::table('inco_nutritional_values')->truncate();
|
|
Product::all()->map(function (Product $product) {
|
|
$product->deleteOrFail();
|
|
});
|
|
$ranges = Range::all();
|
|
Product::factory(150)->make()->each(function (Product $product) use ($ranges) {
|
|
\mt_srand();
|
|
if (\mt_rand(0, 2)) {
|
|
$product->range()->associate($ranges->random(1)->first());
|
|
$product->saveOrFail();
|
|
$product->fresh();
|
|
/** @var \App\Models\IncoNutritionalValue $incValue */
|
|
$incValue = IncoNutritionalValue::factory(1)->make()->get(0);
|
|
$incValue->product()->associate($product);
|
|
$incValue->saveOrFail();
|
|
}
|
|
});
|
|
}
|
|
}
|