ldc-flucollector/database/seeders/ServiceSeeder.php
2023-09-20 14:05:56 +02:00

31 lines
782 B
PHP

<?php
namespace Database\Seeders;
use App\Models\ArcadiaBrand;
use App\Models\Service;
use Illuminate\Database\Seeder;
class ServiceSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run(): void
{
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Service> $services */
$services = Service::factory(5)->make();
$brands = ArcadiaBrand::all();
// * Associate brands to services
$services->map(function (Service $service) use (&$brands) {
$service->saveOrFail();
$pullBrands = $brands->random(rand(1, 2));
$service->arcadiaBrands()->sync($pullBrands);
$brands = $brands->diff($pullBrands);
});
}
}