*/ class OrganizationFactory extends Factory { /** * Define the model's default state. * * @return array */ public function definition(): array { return [ 'name' => fake()->unique()->company(), 'type' => WorkspaceType::Organization, '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) => [ 'type' => WorkspaceType::Personal, 'personal_workspace' => true, ]); } /** * Indicate that the organization is a shared family workspace. */ public function family(): static { return $this->state(fn (array $attributes) => [ 'type' => WorkspaceType::Family, 'personal_workspace' => false, ]); } /** * 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, ]); } /** * Give the organization the plan with the given key. */ public function withPlan(string $key): static { return $this->afterCreating(function (Organization $organization) use ($key): void { $planId = Plan::query()->where('key', $key)->value('id'); if ($planId !== null) { $organization->forceFill(['plan_id' => $planId])->save(); } }); } /** * 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]); }); } }