Laravel multi-tenancy – Single database Free Tutorial Download
Multi-tenancy concept has encountered the laravel community recently and made some noise due to the fact it’s going to make your life easier
— if you already know what tenancy is, just jump to the commands section.
imagine the following situation :
— We have an application that provides a company platform for each registered entity which may contain of projects,members etc..
You can just imagine how painful it’s to filter to get something through your database, and the preparation through your URL to get a specific thing of company, for example the URL can be like this
http://localhost:8080/api/company/company-name/project/project-name, and that’s just the beginning , imagine once the api development gets more complex, you will have to retrieve many things through url to get the thing you are seeking for, but in case of multi-tenant the URL can be like this
- you didn’t have to retrieve the company to just get the a specific project,
- in this scenario you won’t have to check if the user owns this project or not and is the client member of this company to get this project.
weird huh ? Let’s dig into the benefits of multi-tenancy and the trade-off for this solution.
First things first what’s multi-tenancy ?
— Multi-Tenancy concept
A multi-tenant application is having the same codebase but it serves each client as if the application is just made for this tenant, so every single record you have inside your database belongs to this client, which means in essence that each tenant in some sense rented a database for his application which in our scenario his company.
— Multi-tenancy benefits
with that said, if each client of your application has a database that serves only him/her, this will make your code neater as you don’t have to filter or put much codebase just for the preparation as we have encountered earlier to just get a project of specific company, and for a security concern imagine having a problem with one of this clients somehow, the problem only affects the database belongs to this client, so you don’t lose rest of your data which belongs to the other clients.
In essence multi-tenancy helps you to have the following
- get rid of the boring preparation while fetching data.
- more security as each client has his/her own database.
Now, Let’s dig into the interesting part that you’re probably wondering about, how to manage multi-databases while laravel only provides one .env file
Well there is two types of multi-tenancy, you can handle the multi-tenancy with single database and with multi-database as each database serves a particular client in your application.
Multi-tenancy single database
we are still in our scenario, we try to retrieve projects of a company, and check if the authenticated user can really access this project, how can we achieve that, here where sessions,middleware and scopes come into the play.
Here what happens, a user logs in and creates a company / or list the companies he’s already member of, and once he hits any of this company, we get this company and set it to a session so we don’t have to bother ourselves anymore with which company we are dealing with, of course we won’t do this at each endpoint you have because this might be painful, but instead of that we will just create a middleware for that, once you hit a company we get this company we set the session with this company as the tenant of the application, that’s how the middleware should look like, if it’s not that clear yet.