| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 | <?php
namespace Database\Factories\Common;
use App\Models\Common\Contact;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
 * @extends Factory<Contact>
 */
class ContactFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     */
    protected $model = Contact::class;
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'company_id' => 1,
            'first_name' => $this->faker->firstName,
            'last_name' => $this->faker->lastName,
            'email' => $this->faker->unique()->safeEmail,
            'phones' => $this->generatePhones(),
            'is_primary' => $this->faker->boolean(50),
            'created_by' => 1,
            'updated_by' => 1,
        ];
    }
    protected function generatePhones(): array
    {
        $phones = [];
        if ($this->faker->boolean(80)) {
            $phones[] = [
                'data' => ['number' => $this->faker->phoneNumber],
                'type' => 'primary',
            ];
        }
        if ($this->faker->boolean(50)) {
            $phones[] = [
                'data' => ['number' => $this->faker->phoneNumber],
                'type' => 'mobile',
            ];
        }
        if ($this->faker->boolean(30)) {
            $phones[] = [
                'data' => ['number' => $this->faker->phoneNumber],
                'type' => 'toll_free',
            ];
        }
        if ($this->faker->boolean(10)) {
            $phones[] = [
                'data' => ['number' => $this->faker->phoneNumber],
                'type' => 'fax',
            ];
        }
        return $phones;
    }
    public function primary(): self
    {
        return $this->state(function (array $attributes) {
            return [
                'is_primary' => true,
            ];
        });
    }
    public function secondary(): self
    {
        return $this->state(function (array $attributes) {
            return [
                'is_primary' => false,
            ];
        });
    }
}
 |