Files
mytodos/database/factories/OrganizationFactory.php
T

60 lines
1.5 KiB
PHP
Raw Normal View History

2026-10-04 20:34:19 -07:00
<?php
namespace Database\Factories;
use App\Models\Organization;
use App\Models\User;
use App\OrganizationRole;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Organization>
*/
class OrganizationFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->unique()->company(),
'personal_workspace' => false,
'lifetime_access' => false,
];
}
/**
* Indicate that the organization is a single user's personal workspace.
*/
public function personal(): static
{
return $this->state(fn (array $attributes) => [
'personal_workspace' => true,
]);
}
/**
* Give the organization perpetual access, as granted to accounts that
* predate subscriptions.
*/
public function withLifetimeAccess(): static
{
return $this->state(fn (array $attributes) => [
'lifetime_access' => true,
]);
}
/**
* Add the given user to the organization with the given role.
*/
public function withMember(User $user, OrganizationRole $role = OrganizationRole::Owner): static
{
return $this->afterCreating(function (Organization $organization) use ($user, $role): void {
$organization->users()->attach($user, ['role' => $role->value]);
});
}
}