File "UserFactory.php"

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

<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Enums\UserGenderEnum;
use App\Models\City;
use App\Models\Country;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
 */
final class UserFactory extends Factory
{
    /**
     * The current password being used by the factory.
     */
    private static ?string $password = null;

    /**
     * Define the model's default state.
     *
     @return array<model property of \App\Models\User, mixed>
     */
    public function definition(): array
    {

        $userGender = array_map(fn($status) => $status->value, UserGenderEnum::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 [
            'username' => $this->faker->userName(),
            'first_name' => $this->faker->firstName(),
            'last_name' => $this->faker->lastName(),
            'gender' => $this->faker->randomElement($userGender),
            'email' => fake()->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(),
            'email_verified_at' => now(),
            'password' => self::$password ??= Hash::make('password'),
            'remember_token' => Str::random(10),
            'timezone' => $this->faker->timezone(),
            'opt_in' => $this->faker->boolean(false),
            'is_active' => true,
        ];
    }

    /**
     * Indicate that the model's email address should be unverified.
     */
    public function unverified(): static
    {
        return $this->state(fn (array $attributes): array => [
            'email_verified_at' => null,
        ]);
    }
}