57 lines
1.6 KiB
PHP
57 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\License;
|
|
use App\Models\Role;
|
|
use App\Models\Tenant;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class UserFactory extends Factory
|
|
{
|
|
protected static ?string $password;
|
|
|
|
public function definition(): array
|
|
{
|
|
return [
|
|
'name' => fake()->name(),
|
|
'email' => fake()->unique()->safeEmail(),
|
|
'email_verified_at' => now(),
|
|
'password' => static::$password ??= Hash::make('password'),
|
|
'remember_token' => \Illuminate\Support\Str::random(10),
|
|
'tenant_id' => Tenant::factory(),
|
|
];
|
|
}
|
|
|
|
public function withRole(string $roleName): static
|
|
{
|
|
return $this->afterCreating(function ($user) use ($roleName) {
|
|
$role = Role::firstOrCreate(['name' => $roleName]);
|
|
\DB::table('role_user')->insertOrIgnore([
|
|
'role_id' => $role->id,
|
|
'user_id' => $user->id,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
});
|
|
}
|
|
|
|
public function withLicense(License $license): static
|
|
{
|
|
return $this->afterCreating(function ($user) use ($license) {
|
|
\DB::table('license_assignments')->insertOrIgnore([
|
|
'license_id' => $license->id,
|
|
'user_id' => $user->id,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
});
|
|
}
|
|
|
|
public function unverified(): static
|
|
{
|
|
return $this->state(fn () => ['email_verified_at' => null]);
|
|
}
|
|
}
|