ldc-crm/database/seeders/CustomerSeeder.php

48 lines
1.7 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Answer;
use App\Models\Brand;
use App\Models\Customer;
use App\Models\CustomerInfo;
use App\Models\CustomerLogin;
use Illuminate\Database\Seeder;
class CustomerSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run(): void
{
$hasSetAtLeasOneActivated = false;
Customer::factory(10)->create()->each(function ($customer) use (&$hasSetAtLeasOneActivated) {
/** @var \App\Models\Brand */
foreach (Brand::all() as $brand) {
CustomerInfo::factory(1)->make()->each(
function ($customerInfo) use ($customer, $brand) {
$customerInfo->customer()->associate($customer);
$customerInfo->brand()->associate($brand);
$customerInfo->save();
}
);
CustomerLogin::factory(1)->make()->each(
function ($customerLogin) use ($customer, $brand, &$hasSetAtLeasOneActivated) {
$customerLogin->customer()->associate($customer);
$customerLogin->brand()->associate($brand);
if (!$hasSetAtLeasOneActivated) {
$hasSetAtLeasOneActivated = true;
$customerLogin->status = true;
}
$customerLogin->save();
}
);
}
$answerModels = Answer::inRandomOrder()->take(rand(1, 3))->get()->modelKeys();
$customer->answers()->attach($answerModels);
});
}
}