trypost/app/Models/Traits/HasWorkspace.php
Paulo Castellano 2da15df96c feat: introduce Account entity as billing owner and refactor architecture
- Create Account model as Cashier Billable entity (stripe, plan, subscription)
- Account owns workspaces and has an owner_id (User)
- User belongs to one Account via account_id
- Workspace belongs to Account via account_id, no longer has billing fields
- Remove Brand model entirely (workspaces serve as grouping)
- Rename brand_limit to workspace_limit in plans
- Workspace roles simplified: admin/member/viewer (owner via Account)
- Invites now belong to Account with workspaces JSON array
- Pennant features scope changed from Workspace to Account
- EnsureSubscribed middleware checks Account subscription
- All controllers updated: BillingController, OnboardingController,
  WorkspaceInviteController, SocialController, StripeEventListener
- Frontend: extract GoogleAuthButton component, create WorkspaceRole
  enum for type-safe role checks, fix all views for new architecture
- All 1101 tests passing
2026-04-14 22:22:04 -03:00

54 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models\Traits;
use App\Models\Workspace;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
trait HasWorkspace
{
/**
* Get all workspaces the user belongs to (as owner or member).
*/
public function workspaces(): BelongsToMany
{
return $this->belongsToMany(Workspace::class, 'user_workspace')
->withPivot('role')
->withTimestamps();
}
/**
* Get the user's current workspace.
*/
public function currentWorkspace(): BelongsTo
{
return $this->belongsTo(Workspace::class, 'current_workspace_id');
}
/**
* Switch to a different workspace.
*/
public function switchWorkspace(Workspace $workspace): void
{
$this->update(['current_workspace_id' => $workspace->id]);
}
/**
* Check if user belongs to a workspace (owner or member).
*/
public function belongsToWorkspace(Workspace $workspace): bool
{
return $this->workspaces()->where('workspaces.id', $workspace->id)->exists();
}
/**
* Get the count of workspaces the user owns.
*/
public function ownedWorkspacesCount(): int
{
return Workspace::where('user_id', $this->id)->count();
}
}