This article breaks down how I’ve used this package in real production systems, what worked, what didn’t, and the lessons I learned.This article breaks down how I’ve used this package in real production systems, what worked, what didn’t, and the lessons I learned.

Building Scalable SaaS: My Real-World Journey Using spatie/laravel-multitenancy for Multi-Tenant Arc

Introduction

Over the past few months, I’ve built multiple SaaS applications that required full multi-tenant capabilities - including regression application, HR platforms, finance dashboards, and a payroll system, a multi-tenant compensation analytics platform.

Instead of reinventing the wheel, I leaned heavily on the incredible spatie/laravel-multitenancy package. It gave me a solid, reliable, and scalable foundation for handling multiple companies (tenants) on a single Laravel codebase, while keeping each tenant’s data fully isolated.

This article breaks down how I’ve used this package in real production systems, what worked, what didn’t, and the lessons I learned building multi-tenant SaaS applications in Laravel.

What is Multi-Tenant Architecture?

A multi-tenant system allows multiple companies (tenants) to use the same application while ensuring:

  • Their data is fully isolated
  • Their resources are separated
  • Their settings, users, and permissions remain independent

Good examples: \n Stripe, HubSpot, Freshdesk, Workday - all multi-tenant SaaS.

Three Types of Multi-Tenant

Single Database, Shared Schema

  • One DB for all tenants
  • Tenant ID used to separate data
  • Easiest to implement
  • Higher risk of accidental data leaks

Single Database, Separate Schema

  • Each tenant has its own schema/tables
  • More isolation
  • Slightly more maintenance

Multiple Database (One per Tenant)

  • each tenant has completely separate DB
  • Maximum security + isolation
  • Perfect for enterprise SaaS
  • Spatie supports this best

For most of the applications and other products I’ve built, I used the “separate database per tenant” model because I prefer stronger data isolation and easier backups.

Why I choose spatie/laravel-multitenancy

Spatie makes multi-tenancy in Laravel simple, flexible, and production-ready. \n Key advantages I was able to highlight are:

  • Supports all three multi-tenant strategies
  • Automatic tenant identification (domain, subdomain, path, or custom logic)
  • Automatic database switching
  • Tenant-aware queues & jobs
  • Tenant-specific migrations + storage directories
  • Fully extendable and customisable
  • Reliable and actively maintained

This package saved me weeks per project .

Setting up Multi-Tenancy

A. Tenant Identification

I use domain-based identification:

class TenantFinder extends SpatieTenantFinder { public function findForRequest(Request $request): ?Tenant { return Tenant::where('domain', $request->getHost())->first(); } }

This automatically switches the application context depending on the domain.

B. Running Tenant Migration

Each tenant has its own database. \n I run migrations like this:

php artisan tenants:migrate

When I add a feature, every tenant gets the update without manual work.

C. Tenant-Aware Queues

Spatie automatically ensures queues run under the correct tenant:

Bus::chain([ new ProcessPayroll($tenant), new GenerateReports($tenant), new ProcessPayment($tenant), ])->dispatch();

This isolates queued jobs per tenant and avoids cross-data issues.

D. Central Database vs Tenant Database

I separate components like this:

Central Database

  • All tenants
  • Subscriptions
  • Billing
  • System-wide settings

Tenant Database

  • Employees
  • Payroll
  • Reports
  • Logs
  • Configurations

This separation has been a huge scaling advantage.

Challenges I Faced (And How I Solved Them)

1. Queue Context Issues

Sometimes queued jobs executed without tenant context

Solution:

Explicitly set tenant in queued jobs:

$this->tenant->makeCurrent();

2. Cache Leakage

Cached values from one tenant were leaking to another.

Solution:

Use tenant-scoped cache:

cache()->tenant($tenant->id)->put();

3. Migration Order Problems

If I ran central migrations after tenant migrations, conflicts appeared.

Solution:

Create a CI pipeline process: \n central migrate → tenant migrate

Best Practices for Multi-Tenant Laravel Systems

  • [ ] Always force tenant context in jobs & events
  • [ ] Use a separate DB per tenant for high-risk / financial system
  • [ ] Add logging for tenant identification issues
  • [ ] Isolate cache, queue, and storage
  • [ ] Build modular services per domain
  • [ ] Never mix tenant & central logic in the same model

This has helped me scale to hundreds of tenants without issues.

When NOT to Use Multi-Tenancy

Avoid multi-tenancy if:

  • The system is for only one organisation
  • The app requires heavy, tenant-level analytics
  • Government/security rules require total infrastructure isolation
  • On-prem installations are required
  • The tech team is very junior

A monolithic single-tenant system might be simpler in these cases.


My Conclusion

Multi-tenant architecture is powerful, cost-effective, and ideal for modern SaaS applications. With spatie/laravel-multitenancy, I’ve been able to deliver multiple production-ready platforms quickly and safely.

Laravel + Spatie gives you the structure, isolation, performance, and flexibility you need to build serious SaaS products without fighting the framework.

If you’re building SaaS in Laravel, this package is one of the best decisions you can make.

\ \

Market Opportunity
MY Logo
MY Price(MY)
$0.1046
$0.1046$0.1046
+1.06%
USD
MY (MY) Live Price Chart
Disclaimer: The articles reposted on this site are sourced from public platforms and are provided for informational purposes only. They do not necessarily reflect the views of MEXC. All rights remain with the original authors. If you believe any content infringes on third-party rights, please contact service@support.mexc.com for removal. MEXC makes no guarantees regarding the accuracy, completeness, or timeliness of the content and is not responsible for any actions taken based on the information provided. The content does not constitute financial, legal, or other professional advice, nor should it be considered a recommendation or endorsement by MEXC.

You May Also Like

X to cut off InfoFi crypto projects from accessing its API

X to cut off InfoFi crypto projects from accessing its API

X, the most widely used app for crypto projects, is changing its API access policy. InfoFi projects, which proliferated non-organic bot content, will be cut off
Share
Cryptopolitan2026/01/16 02:50
X Just Killed Kaito and InfoFi Crypto, Several Tokens Crash

X Just Killed Kaito and InfoFi Crypto, Several Tokens Crash

The post X Just Killed Kaito and InfoFi Crypto, Several Tokens Crash appeared on BitcoinEthereumNews.com. X has revoked API access for apps that reward users for
Share
BitcoinEthereumNews2026/01/16 03:42
Google's AP2 protocol has been released. Does encrypted AI still have a chance?

Google's AP2 protocol has been released. Does encrypted AI still have a chance?

Following the MCP and A2A protocols, the AI Agent market has seen another blockbuster arrival: the Agent Payments Protocol (AP2), developed by Google. This will clearly further enhance AI Agents' autonomous multi-tasking capabilities, but the unfortunate reality is that it has little to do with web3AI. Let's take a closer look: What problem does AP2 solve? Simply put, the MCP protocol is like a universal hook, enabling AI agents to connect to various external tools and data sources; A2A is a team collaboration communication protocol that allows multiple AI agents to cooperate with each other to complete complex tasks; AP2 completes the last piece of the puzzle - payment capability. In other words, MCP opens up connectivity, A2A promotes collaboration efficiency, and AP2 achieves value exchange. The arrival of AP2 truly injects "soul" into the autonomous collaboration and task execution of Multi-Agents. Imagine AI Agents connecting Qunar, Meituan, and Didi to complete the booking of flights, hotels, and car rentals, but then getting stuck at the point of "self-payment." What's the point of all that multitasking? So, remember this: AP2 is an extension of MCP+A2A, solving the last mile problem of AI Agent automated execution. What are the technical highlights of AP2? The core innovation of AP2 is the Mandates mechanism, which is divided into real-time authorization mode and delegated authorization mode. Real-time authorization is easy to understand. The AI Agent finds the product and shows it to you. The operation can only be performed after the user signs. Delegated authorization requires the user to set rules in advance, such as only buying the iPhone 17 when the price drops to 5,000. The AI Agent monitors the trigger conditions and executes automatically. The implementation logic is cryptographically signed using Verifiable Credentials (VCs). Users can set complex commission conditions, including price ranges, time limits, and payment method priorities, forming a tamper-proof digital contract. Once signed, the AI Agent executes according to the conditions, with VCs ensuring auditability and security at every step. Of particular note is the "A2A x402" extension, a technical component developed by Google specifically for crypto payments, developed in collaboration with Coinbase and the Ethereum Foundation. This extension enables AI Agents to seamlessly process stablecoins, ETH, and other blockchain assets, supporting native payment scenarios within the Web3 ecosystem. What kind of imagination space can AP2 bring? After analyzing the technical principles, do you think that's it? Yes, in fact, the AP2 is boring when it is disassembled alone. Its real charm lies in connecting and opening up the "MCP+A2A+AP2" technology stack, completely opening up the complete link of AI Agent's autonomous analysis+execution+payment. From now on, AI Agents can open up many application scenarios. For example, AI Agents for stock investment and financial management can help us monitor the market 24/7 and conduct independent transactions. Enterprise procurement AI Agents can automatically replenish and renew without human intervention. AP2's complementary payment capabilities will further expand the penetration of the Agent-to-Agent economy into more scenarios. Google obviously understands that after the technical framework is established, the ecological implementation must be relied upon, so it has brought in more than 60 partners to develop it, almost covering the entire payment and business ecosystem. Interestingly, it also involves major Crypto players such as Ethereum, Coinbase, MetaMask, and Sui. Combined with the current trend of currency and stock integration, the imagination space has been doubled. Is web3 AI really dead? Not entirely. Google's AP2 looks complete, but it only achieves technical compatibility with Crypto payments. It can only be regarded as an extension of the traditional authorization framework and belongs to the category of automated execution. There is a "paradigm" difference between it and the autonomous asset management pursued by pure Crypto native solutions. The Crypto-native solutions under exploration are taking the "decentralized custody + on-chain verification" route, including AI Agent autonomous asset management, AI Agent autonomous transactions (DeFAI), AI Agent digital identity and on-chain reputation system (ERC-8004...), AI Agent on-chain governance DAO framework, AI Agent NPC and digital avatars, and many other interesting and fun directions. Ultimately, once users get used to AI Agent payments in traditional fields, their acceptance of AI Agents autonomously owning digital assets will also increase. And for those scenarios that AP2 cannot reach, such as anonymous transactions, censorship-resistant payments, and decentralized asset management, there will always be a time for crypto-native solutions to show their strength? The two are more likely to be complementary rather than competitive, but to be honest, the key technological advancements behind AI Agents currently all come from web2AI, and web3AI still needs to keep up the good work!
Share
PANews2025/09/18 07:00