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(); } }