Skip to main content

Overview

When building features in your multi-tenant application, you’ll need to create tables that are automatically scoped to the current team. This guide shows you the pattern for creating tenant-aware tables.

Basic Pattern

Every tenant-scoped table should include a team_id foreign key that references the teams table.

Step 1: Create Migration

Key Elements

  1. Foreign Key: $table->foreignId('team_id')
  2. Constraint: ->constrained() creates the foreign key relationship
  3. Cascade Delete: ->cascadeOnDelete() ensures data cleanup
  4. Index: $table->index(['team_id', 'created_at']) for performance

Step 2: Create Model with Global Scope

Understanding the Global Scope

The global scope does two important things:

1. Automatic Filtering

All queries are automatically filtered by the current team:

2. Automatic Assignment

New records get team_id set automatically:

Common Patterns

Pattern 1: Simple Tenant-Scoped Table

For straightforward tables that belong to a team:

Pattern 2: Nested Tenant-Scoped Table

For tables that belong to other tenant-scoped models:
Model with nested scope:

Pattern 3: Soft Deletes

For tables that need soft deletion:

Pattern 4: Polymorphic Relationships

For tables with polymorphic relationships:

Performance Optimization

Adding Composite Indexes

Always add indexes on team_id combined with frequently queried columns:

Query Optimization

Use eager loading to avoid N+1 queries:

Bypassing Global Scope

Sometimes you need to query without the team scope (e.g., admin panel):

Conditional Scoping

Only apply scope in specific contexts:

Testing

Checklist

When creating a tenant-scoped table, make sure you:
  • Add team_id foreign key column
  • Use constrained()->cascadeOnDelete()
  • Add indexes on [team_id, ...] combinations
  • Include team_id in model’s $fillable
  • Add global scope in booted() method
  • Set team_id automatically in creating event
  • Add team() relationship method
  • Test the scoping behavior
  • Test automatic team_id assignment

Next Steps