Laravel Multitenancy — Part 1

Olawale Adewoyin Bañez
3 min readOct 2, 2015

--

How do you start?

That’s the question I asked myself after developing a Human Resources Software with Laravel 4. In this post, I am writing on how to make sure we have a separate database for each client. Not a database where you have a tenant_id in each of the table. I’m not going to bother you with a whole lot of story, so let’s get down to it.

I have never really posted anything about my thoughts. This is my first time posting and there might be a couple of things missing or yada yada here and there. Please let me know and I’ll definitely get better. :)

Please note that this is not a “THIS IS HOW YOU MUST DO IT”. I’m only sharing my ideas of how I accomplished this. I’m very positive we are going to have people who will critic some of the code I show, however, I’m also learning, so please I am not preaching.

Database

We need the main database or main section where we will store data such as the users, tenants, tenant_databases, and tenant_users. I have more tables in my AdminDB however, I will only touch on these few.

The tenant’s table will have the company name and if you want the sub-domain name as well. The tenant_database table will be used to store the dynamically generated database name. Here is a listing of some of the code/migration files.

Once you have this setup, your master db containing the users, tenants, tenant_users and tenant_database(s) is completed. I have a database_type_id because I use encryption_keys and encryption_ivs to encrypt personal data. And I keep them separate from the main database.

The .env file

The env file is where you save certain things you want to hide from prying eyes, I will show an example of how mine is setup for the database connections.

Database config file

For every connection, I make sure I separate them. I don’t want to keep changing connections here and there. This is as follows…

Note that the default connection is pointed to the main database (admin database). In this case benta.

Once all these is setup, you can start fleshing out the stub/template database that your tenant will have. I have another application laravel/lumen that creates the database for the tenant.

Once a user signs up, an event is fired in the system to send an email to the user, etc. This is how my EventServiceProvider in app/Providers/EventServiceProvider looks like for registration purposes. Note that these events take place when saving the user, tenant, tenant_user and tenant_database records.

The ProvisionTenantDatabases class handles calling the micro-service to provision the database. You have to supply the service the name of the database, username (which will be derived from the subdomain name of the tenant) and a randomly generated password. See below…

The env key BENTANAMIN_DATABASE_HOST is already in my environment file and it points to the host system of where I want my tenant’s database to be created. However note that the user will be connecting from the web application server to the database host. I will not touch on this, however if someone needs help on that, please let me know.

The code that handles calling the micro-services uses Guzzle\Service\Client to make calls. I only post the database host, database_name, username, password and a migrate variable which is either 1 for the normal tenant database and 0 for the database where we store the encryption_keys.

See part 2 below:

--

--

Olawale Adewoyin Bañez

Love the Programming world. Enterprise and Gaming. Work at ICGroup, Winnipeg, Manitoba, Canada