Laravel Teamwork – Manage teams in multi-tenant apps

To manage teams in multi-tenant apps in Laravel, you can use a package like Laravel Teamwork to handle team-based permissions, team ownership, and switching between teams.

Step 1: Install Laravel Teamwork Package

First, install the Laravel Teamwork package via Composer:

bash

 

Step 2: Publish Configuration and Migration Files

Publish the configuration file and migration files using the following command:

bash

This will publish the necessary configuration file config/teamwork.php and the migration files to create teams and team-user relationships.

Step 3: Run Migrations

Run the migration to create the required tables for managing teams:

bash

This will create the following tables:

  • teams: Stores team details (name, owner, etc.)
  • team_user: Pivot table for many-to-many relationship between users and teams
  • invitations: Stores team invitations for users who are invited to join a team

Step 4: Add the Teamwork Trait to the User Model

To enable team functionality, add the Teamwork trait to your User model. Open app/Models/User.php and update it as follows:

php

This will allow users to join teams, switch teams, and manage team relationships.

Step 5: Create a Team Model

Next, you need to create a Team model to represent the teams in your application. Run the following Artisan command to create the Team model:

bash

 

In app/Models/Team.php, add the following code to define the team relationships:

php

 

Step 6: Create Teams and Assign Users

You can now create teams and assign users to them using the provided methods.

Example: Creating a Team

To create a team and assign a user as the owner:

php

Example: Add User to a Team

To add another user to the team:

php

 

Step 7: Handle Team Invitations (Optional)

The Laravel Teamwork package provides a built-in way to handle team invitations. To invite a user to a team, you can use the Teamwork::inviteToTeam() method.

Example: Inviting a User to a Team

php

Example: Accepting an Invitation

Once a user receives the invitation, they can accept it by following a link (usually embedded in the invitation email). Here’s how you can handle invitation acceptance:

php

 

Step 8: Switching Between Teams

Since a user can belong to multiple teams, Laravel Teamwork provides methods to switch between teams easily.

Example: Switch Team

To switch the current team for a user:

php

This will set the current active team for the user in the session.

Step 9: Middleware for Team-based Authorization

You can use the team-based middleware provided by Laravel Teamwork to restrict access to certain routes or resources based on the user’s current team.

Example: Middleware to Ensure User Belongs to a Team

In your routes/web.php file, you can apply the team middleware:

php

You can customize the middleware to ensure that users have the right roles within a team or access to specific teams.

Step 10: Displaying Teams and Users in Views

You can display the teams a user belongs to, their current team, and allow them to switch teams in your views.

Example: Display User’s Teams

php

Example: Show Current Team

php

Example: Switch Team Form

You can create a simple form to allow users to switch teams:

php

In your controller, handle the team switch logic:

php

 

Step 11: Customize Teamwork Configuration (Optional)

You can customize how teams work in your application by editing the config/teamwork.php file, which was published earlier. You can adjust the model names, table names, and other team-related settings.


Summary

With Laravel Teamwork, you can efficiently manage teams within your multi-tenant application, allowing users to belong to multiple teams, switch teams, and handle team-based permissions. Here’s a quick recap of the steps:

  1. Install Laravel Teamwork via Composer.
  2. Run migrations to create the necessary tables.
  3. Add the Teamwork trait to the User model.
  4. Create teams and assign users.
  5. Handle team invitations.
  6. Allow users to switch between teams.
  7. Use middleware to enforce team-based authorization.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top