Overview
Theteam_user table is a pivot table that manages the many-to-many relationship between teams and users. It tracks which users are members of which teams.
Schema
Columns
id (Primary Key)
- Type: BIGINT UNSIGNED
- Description: Unique identifier for the membership
- Auto-increment: Yes
team_id
- Type: BIGINT UNSIGNED
- Description: Foreign key to teams table
- Required: Yes
- Constraints:
- Foreign key to
teams.id - Cascade on delete
- Foreign key to
user_id
- Type: BIGINT UNSIGNED
- Description: Foreign key to users table
- Required: Yes
- Constraints:
- Foreign key to
users.id - Cascade on delete
- Foreign key to
created_at / updated_at
- Type: TIMESTAMP
- Description: When user joined the team
- Automatically managed: Yes
Unique Constraints
Composite Unique Index
Prevents duplicate memberships:Usage Examples
Adding a Member to a Team
Removing a Member from a Team
Checking Membership
Getting Team Members
Getting User’s Teams
Query Performance
Eager Loading
Always eager load relationships to avoid N+1 queries:Indexes
The foreign keys automatically create indexes, but you can add more:Adding Additional Pivot Data
You can add extra columns to store membership-specific data:Migration
Model Configuration
Usage
Cascade Behavior
When teams or users are deleted:Team Deleted
Allteam_user records for that team are automatically deleted:
User Deleted
Allteam_user records for that user are automatically deleted:
Testing
Next Steps
- Team Invitations Table - Invite system
- Managing Members - UI for member management

