Implementing SaaS (Multi Tenant) Model

Making separate databases per customer sounds like an attractive thing at first. However, if you plan on having thousands of customers or more, these systems can introduce a non-trivial amount of complexity around schema management and operational complexity. Another approach I’ve found useful is to make all your primary keys composite keys that include the ‘tenant id’. All your model associations end up joining with this ‘tenant id’, and you can implement behaviors that ensure the tenant id is always added to queries.

If you are hard set on using multiple databases, you can use ConnectionManager::create() to define new connections at runtime.

3 Likes