NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Teams for Laravel

Model traits

Members are stored polymorphically, so any Eloquent model can join a team — a user, a guest account, an API client. Add the HasTeams trait to the model that acts as a member to get the membership, ownership, role and permission helpers:

use Illuminate\Foundation\Auth\User as Authenticatable;
use RoundlyConsulting\Teams\Traits\HasTeams;

class User extends Authenticatable
{
    use HasTeams;
}

The HasTeams API

$user->teams();                                    // MorphMany<Member> — the model's memberships
$user->ownedTeams();                               // MorphMany<Team> — teams this model owns
$user->hasTeam();                                  // belongs to any team
$user->belongsToTeam($team);                       // is a member of $team
$user->isMemberOf($team);                          // alias of belongsToTeam()
$user->ownsTeam($team);                            // is the owner of $team
$user->teamsWithRole('admin');                     // Collection<Member> holding that role
$user->teamRole($team);                            // ?Role — the resolved role on $team
$user->hasTeamRole($team, 'admin');                // bool
$user->hasTeamPermission($team, 'billing.manage'); // bool

teams() returns the model’s memberships — Member rows, each with its team and role — not Team models. Use Team::query()->withMember($user) when you need the teams themselves.

A current team with BelongsToTeam

For apps with a team switcher, BelongsToTeam gives a model a single current team through a team_id column. It sits happily next to HasTeams:

use Illuminate\Foundation\Auth\User as Authenticatable;
use RoundlyConsulting\Teams\Traits\BelongsToTeam;
use RoundlyConsulting\Teams\Traits\HasTeams;

class User extends Authenticatable
{
    use BelongsToTeam;   // team() + switchTeamTo()
    use HasTeams;        // memberships, roles, permissions
}

$user->team;                  // the current Team (BelongsTo)
$user->switchTeamTo($team);   // associates + saves, returns $user

The column lives on your own table, so the package doesn’t ship a migration for it — add a nullable team_id yourself:

Schema::table('users', function (Blueprint $table): void {
    $table->foreignId('team_id')->nullable()->constrained();
});

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.