---
title: "Nested teams — Teams for Laravel | Roundly"
description: "Optional company → department → team hierarchies: move teams, walk the tree, cap the depth and let roles held above apply below."
url: https://roundly-consulting.com/open-source/docs/teams-for-laravel/nested-teams
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)

# Nested teams

For hierarchies such as company → department → team. Nesting is off by default — set TEAMS\_NESTING=true after the parent\_id migration ran (see Installation); while it is off nothing reads the column. Membership stays per team:

```php
use RoundlyConsulting\Teams\Facades\Teams;

// .env → TEAMS_NESTING=true (after the parent_id migration ran)
Teams::for($department)->moveUnder($company);   // Team — place under a parent
Teams::for($department)->moveToRoot();          // Team — detach

$department->parent;         // ?Team
$company->children;          // Collection<Team>
$team->ancestors();          // nearest first; stops at a soft-deleted ancestor
$company->descendants();     // level by level
$team->root();               // the topmost ancestor, or the team itself
$team->depth();              // 0 for a root team
```

moveUnder() and moveToRoot() run MoveTeamAction, which locks both rows for the transaction, saves and fires TeamMoved with the team, the new parent and the previous parent key; Teams::fake() records it. Every parent change is validated on save — through the action or a direct $team->update(\['parent\_id' => …\]) — and throws TeamHierarchyException when nesting is off, when the new parent is the team itself or one of its descendants (soft-deleted ones included), or when any team of the moved subtree would sit deeper than nesting.max\_depth (default 4, 1–100; the root is depth 0). Detaching is always allowed.

Force-deleting a team that still has child teams fails — move or delete them first. Soft-deleting a parent keeps the link but stops ancestors() and role inheritance, since a trashed team grants nothing. Every walk is bounded by max\_depth and remembers visited rows, so even data corrupted into a loop by a raw query cannot spin forever.

## Roles inherit down

With TEAMS\_INHERIT\_ROLES\_DOWN=true — effective only with nesting on — a role held on a team also applies on every team below it, never above:

- Inherits — memberHasPermission() and hasTeamPermission(), resolved against the role set of the team the role is held on; memberHasRole(), hasTeamRole() and @teamRole; the teams.<permission> Gate abilities, @teamPermission and team policies; isAccessibleBy() and so the teams.context middleware, switchTo(), setCurrentMany() and the queued-job re-check; with the permissions bridge, grants in a team above.
- Does not inherit — hasActiveMember(), hasMember(), findMember(), isActiveMemberOf(), teamRole(), belongsToTeam() and isMemberOf(): membership stays per team. Ownership — isOwnedBy(), teams.owner, @teamOwner and the policy before() — is per team too, though a parent owner’s owner role permissions still inherit.
- Expired memberships, and ones that have not started yet, never count on any level.

Team-scoped data usually points at the root team:

```php
// Team-scoped data usually points at the root team:
Teams::switchTo($team->root());
```

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