A comprehensive Laravel package for managing workspaces, invitations, and member roles.
composer require whilesmart/eloquent-workspaces
php artisan migrateThat's it! The package will auto-register routes and work out of the box.
All package configuration is environment-driven. Add these variables to your .env file:
# The user model class that will be used for workspace relationships.
WORKSPACES_USER_MODEL=App\\Models\\User
# The workspace model class. Point this at your own subclass (extending the
# base Workspace model) to compose extra traits/behaviour; it is honoured by
# the HasWorkspaces relations, role-context queries, and route-model binding.
WORKSPACES_MODEL=App\\Models\\Workspace
# Enable or disable route registration (default: true)
WORKSPACES_REGISTER_ROUTES=true
# Route prefix for all workspace endpoints (default: api)
WORKSPACES_ROUTE_PREFIX=api// config/workspaces.php
'workspace_model' => App\Models\Workspace::class,use Whilesmart\Workspaces\Models\Workspace as BaseWorkspace;
class Workspace extends BaseWorkspace
{
// compose your own capabilities (HasInvoices, HasBrands, Configurable, …)
}With this set, $user->workspaces(), currentOrDefaultWorkspace(), route-bound
{workspace} params, and role checks all return / use your subclass — no
re-resolving in the host app.
# Automatically create a personal workspace for a user on registration (default: true)
WORKSPACES_AUTO_CREATE=true# Number of days an invitation is valid (default: 7)
WORKSPACES_INVITATION_EXPIRY=7For more advanced configuration, you can publish the configuration file:
php artisan vendor:publish --tag="workspaces-config"This will create a config/workspaces.php file in your application.
You can specify middleware for the workspace routes.
'route_middleware' => ['auth:sanctum'],This template is used to name the personal workspace created for a new user.
Available variables: {first_name}, {last_name}, {full_name}
'personal_workspace_name_template' => "{first_name}'s Workspace",Define the roles available in a workspace.
'roles' => [
'owner' => 'owner',
'admin' => 'admin',
'member' => 'member',
],The {workspace} parameter is resolved by slug.
GET /api/workspaces- List the authenticated user's workspacesPOST /api/workspaces- Create a workspaceGET /api/workspaces/{workspace}- Get a workspacePUT /api/workspaces/{workspace}- Update a workspaceDELETE /api/workspaces/{workspace}- Delete a workspacePOST /api/workspaces/{workspace}/switch- Switch to a workspace
GET /api/workspaces/{workspace}/members- Get workspace membersDELETE /api/workspaces/{workspace}/members/{userId}- Remove a member from a workspacePOST /api/workspaces/{workspace}/leave- Leave a workspace
POST /api/workspaces/{workspace}/members/invite- Invite a member to a workspaceGET /api/workspaces/{workspace}/invitations- List pending invitationsDELETE /api/workspaces/{workspace}/invitations/{invitation}- Cancel a pending invitationPOST /api/workspaces/invitations/{token}/accept- Accept an invitationPOST /api/workspaces/invitations/{token}/decline- Decline an invitation
Accept and decline are keyed on the invitation token (the value carried in the invite). The authenticated user's email must match the address the invitation was sent to.
The package dispatches the following events, which a host application can listen for:
MemberInvited- an invitation was created (carries the workspace and the invitation). Listen for this to deliver the invitation email.MemberJoined- an invited user accepted and joined (carries the workspace, the member, and the role).WorkspaceSwitched- a user switched their current workspace (carries the new workspace, the user, and the previous workspace).
You can publish the package's assets using the following commands:
# Publish only configuration
php artisan vendor:publish --tag="workspaces-config"
# Publish only migrations
php artisan vendor:publish --tag="workspaces-migrations"
# Publish everything
php artisan vendor:publish --provider="Whilesmart\\Workspaces\\WorkspacesServiceProvider"This project is licensed under the MIT License - see the LICENSE file for details.