Skip to main content

Real-World Scenario

You’re building a SaaS platform where companies subscribe to use your software. Think tools like Slack, Notion, or Trello. Each company (team) needs to:
  • Track their subscription and usage limits
  • Monitor API usage and quota consumption
  • Manage team members and roles
  • View analytics and insights
  • Store application-specific data
This example shows a complete, production-ready SaaS implementation with usage tracking, quota enforcement, and subscription management.

The Business Model

SaaS Structure:
  • Each company gets their own workspace (team)
  • Multiple pricing tiers with different feature limits
  • Usage-based quota tracking (API calls, storage, etc.)
  • Per-team billing with automatic quota resets
  • Self-service upgrade/downgrade
What Teams Get:
  • Dedicated workspace for their company
  • Configurable usage limits based on plan
  • Real-time usage monitoring
  • Team collaboration features
  • API access with rate limiting

Database Structure

1. Subscriptions Table (Built-in)

Laravel Cashier provides this, but here’s what it tracks:

2. Usage Metrics Table

Track consumption for quota enforcement:

3. API Keys Table

For programmatic access to your SaaS platform:

4. Activity Logs Table

Track important actions for audit trails:

Complete Model Implementation

UsageMetric Model with Quota Enforcement

ApiKey Model with Secure Key Management

ActivityLog Model

Filament Resources

Usage Metrics Widget (Dashboard)

API Keys Resource

Middleware for Quota Enforcement

Create middleware to check quotas before allowing actions:
Register in bootstrap/app.php:
Use in routes:

Why This Example is Better

Real SaaS Features

  • ✅ Usage tracking with automatic quota enforcement
  • ✅ Multiple metric types (API calls, storage, users, etc.)
  • ✅ Automatic period resets for billing cycles
  • ✅ API key management with scopes and expiration
  • ✅ Activity logging for audit trails
  • ✅ Plan-based quota limits

Production-Ready Implementation

  • ✅ Secure API key hashing
  • ✅ Middleware for automatic quota checks
  • ✅ Visual quota indicators with color coding
  • ✅ Proper relationship management
  • ✅ Automatic team scoping on all models

Business Logic

  • ✅ Quota percentage calculations
  • ✅ Plan-based limit configuration
  • ✅ Expired key detection
  • ✅ Usage color indicators (green/yellow/red)
  • ✅ Automatic usage resets

User Experience

  • ✅ Real-time usage dashboard
  • ✅ Clear quota warnings at 80% and 100%
  • ✅ API key masking for security
  • ✅ Activity logs for transparency
  • ✅ One-click API key revocation

Next Steps