Skip to main content

Overview

The team_invitations table manages email invitations to join teams. It stores pending, accepted, and expired invitations.

Schema

Columns

id (Primary Key)

  • Type: BIGINT UNSIGNED
  • Description: Unique identifier for the invitation
  • 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

email

  • Type: VARCHAR(255)
  • Description: Email address of the invitee
  • Required: Yes
  • Example: “[email protected]

role

  • Type: VARCHAR(255)
  • Description: Role to assign when invitation is accepted
  • Required: No
  • Example: “admin”, “editor”, “viewer”

token

  • Type: VARCHAR(64)
  • Description: Unique secure token for the invitation link
  • Required: Yes
  • Unique: Yes
  • Generated: Using Str::random(64)

expires_at

  • Type: TIMESTAMP
  • Description: When the invitation expires
  • Required: Yes
  • Default: 7 days from creation
  • Example: now()->addDays(7)

accepted_at

  • Type: TIMESTAMP
  • Description: When the invitation was accepted
  • Required: No
  • Null: Indicates pending invitation

created_at / updated_at

  • Type: TIMESTAMP
  • Description: Laravel timestamps
  • Automatically managed: Yes

Indexes

Composite Index

  • Faster lookups when checking existing invitations
  • Used for validating duplicate invitations

Unique Token Index

  • Ensures each invitation has a unique URL
  • Prevents token collisions

Usage Examples

Creating an Invitation

Finding an Invitation

Accepting an Invitation

Checking Invitation Status

Resending an Invitation

Query Scopes

Add helpful scopes to the TeamInvitation model:

Usage

Cascade Behavior

Team Deleted

All invitations for that team are automatically deleted:

Security Considerations

Token Generation

Always use cryptographically secure random tokens:

Token Length

64 characters provides sufficient entropy to prevent brute-force attacks.

Expiration

Always set an expiration date to limit invitation validity:

One-Time Use

Only find invitations that haven’t been accepted:

Cleaning Up Old Invitations

Manual Cleanup

Scheduled Cleanup

Create an Artisan command:
Schedule it:

Testing

Next Steps