File "CustomerFactory.php"

Full Path: /home/tecassol/public_html/tecas-solar.ma/database/factories/CustomerFactory.php
File size: 1.32 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace Database\Factories;

use App\Enums\CustomerTypeEnum;
use App\Models\City;
use App\Models\Country;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Customer>
 */
class CustomerFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $customerType = array_map(fn ($status) => $status->value, CustomerTypeEnum::cases());
        $country = Country::inRandomOrder()->first() ?? Country::factory()->create();
        $city = City::where('country_id', $country->id)->inRandomOrder()->first() ?? City::factory()->create(['country_id' => $country->id]);
        return [
            'first_name' => $this->faker->firstName(),
            'last_name' => $this->faker->lastName(),
            'email' => $this->faker->unique()->safeEmail(),
            'phone_number' => $this->faker->phoneNumber(),
            'address' => $this->faker->address(),
            'city_id' => $city->id,
            'country_id' => $country->id,
            'postal_code' => substr($this->faker->postcode(), 0, 5),
            'birth_date' => $this->faker->date(),
            'customer_type' => $this->faker->randomElement($customerType),
        ];
    }
}