Implementing Laravel Multi Tenant Architecture

Before two years ago, We ( me and Team Romin Interactive ) have developed online resort booking system – similar to hotel booking system, but a little bit of spicy requirement in it. The complete system was developed in PyroCMS 2.2.1 and hack of FireSale eCommerce module available for PyroCMS. However, we would like to have it developed in Laravel from the scratch for multiple clients, and therefore we planned to implement everything in a way of laravel multi tenant with proper architecture that is scalable.

Skip to Implementation or Read my story and inspiration

It’s been a great project and is being used by multiple resorts for running their business, and It has been so successful that they have completely moved on to the online booking system, We feel proud to see our system used worldwide.

But to be honest and being a geeky man and always experimenting, I looked into a code, and It was a complete mess – sorry! and I decided to rewrite complete application to Laravel framework and journey begins…

Understanding Multi tenant

Now fast forward a couple of months…We have finished the rewriting of booking system and It’s now time to deploy the system for multiple resorts. And we decided to use multi tenant architecture and there are mainly two ways of achieving it as we know

  • Single Code, Single DB ( completely powered by complex relations and foreign keys )
  • Single Code, Multiple DB

The problem we actually had is how we do it in laravel multi tenant way and we researched a lot and found no WOW resource, so we started R&D and tried to find a solution. Now. one of my good buddy and good coder Bhagirath Dangar suggested to go wit the first approach, and at one point, I agreed and we implemented, but then after long though process, I felt something will be wrong and we decided to implement second approach

But, we failed to find some good solution as we need to take care of migrations, seeders, various client-level settings and configurations, design changes and stuff like that..and most importantly Security. And finally, we got the answer and actually why “Environments Handling” is available in Laravel, and It’s the only concept supported by everything we were worried about..

So, we made simple rule: 1 Site = 1 Environment

Continue if you are a geek 😉

So, today I am going to share how we did it. It’s actually pretty trick!

Time to implement it

1. Change in bootstrap/start.php

start.php is the file in laravel 4.2 that actually responsible for setting up the environment , so let’s edit a bit.
Open a file and find for the following..

and replace it with

So, what we are doing actually here is, assuming that our domain is actually an environment as stated earlier in formula above.
The code we changed is going to check for a domain name, remove the www. prefix from the domain name, if used, and use other part of domain as environment ( finally wrote a correct spelling without fix )


2. Create the environment folder

Go to app/config folder, and as you might be aware how Laravel handles the environment, create a folder that matches your domain name, probably localhost on your development environment.


3. Copy the required config files to environmental folder

Mainly, you need to copy the database.php, app.php, and mail.php and change the configuration values as required. For now, I suggests these 3 files only as those are the core files to any instance and will not be shared among each other but you are free to copy any other config file as required in your case.


4. Use “–env” for everything related to Database

Now whenever you fire up command that are related to database such as migration:install, migrate, db:seed etc..just append the –env options available to it, for an example:

php artisan migrate:install


And if everything you did is as I said, you should be good to go. Create some virtual hosts and test it out. However, the last part is pretty risky, and you can not afford to forget appending the –env options to the command, as it may lead the mis configuration to default instance you have installed.


That’s it for now. Happy Coding!

Author: Daksh Mehta

I am professional web developer and programmer, having experience of more then 6 year in developing professional websites. In 2008, I created - a group of computer Internet geeks who are expertise in providing world class web solutions to all around the world. I am also newly became online marketer.