---
title: "Model traits — Teams for Laravel | Roundly"
description: "HasTeams gives any model memberships, ownership, role and permission helpers; BelongsToTeam adds a switchable current team."
url: https://roundly-consulting.com/open-source/docs/teams-for-laravel/model-traits
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Teams for Laravel](https://roundly-consulting.com/open-source/docs/teams-for-laravel.md)

# 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:

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

class User extends Authenticatable
{
    use HasTeams;
}
```

## The HasTeams API

```php
$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:

```php
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:

```php
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](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## 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.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/teams-for-laravel.md)
