<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use App\Models\Role;
class UserFactory extends Factory
{
protected $model = \App\Models\User::class;
public function definition(): array
{
return [
'first_name' => $this->faker->firstName(),
'last_name' => $this->faker->lastName(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => Hash::make('password'),
'phone' => $this->faker->phoneNumber(),
'role_id' => Role::factory(),
'remember_token' => $this->faker->md5(),
];
}
public function admin()
{
return $this->state(function (array $attributes) {
return [
'role_id' => Role::where('name', 'admin')->first() ?? Role::factory()->create(['name' => 'admin']),
];
});
}
public function user()
{
return $this->state(function (array $attributes) {
return [
'role_id' => Role::where('name', 'user')->first() ?? Role::factory()->create(['name' => 'user']),
];
});
}
}