Nowadays most organizations or companies require Software as a Service (SaaS). In SaaS, software is provided on subscription basis where software is being located on external server instead of server located in-house.
In SaaS model users can access programs via the internet instead of each user having to install the software on their own system, A SaaS can run one instance of its application on one instance of database and provide web access (through username and password) to multiple customers. Most of the SaaS products support Multi-Tenant architecture.
What is Multi-Tenant
In Multi-Tenant architecture multiple single instances of software run on a single physical server. When customer register for the SaaS, system automatically create new instance for the newly added customer within in a minute. Here tenant is a customer who got instance of our program, now tenant can customize their instance as they want. For example, tenants can change the design of user interface, can customize system meeting their business rules, etc without impacting other customers/tenant.
In a multi-tenant architecture, multiple instances of an application operate in a shared environment. This architecture is able to work because each tenant is integrated physically, but logically separated. In other words, a single instance of the software will run on one server and then serve multiple tenants.
To achieve multi-tenancy in Laravel we are using “samuel stancl package”. Using this package we can achieve multi-tenancy in our project with very less and more understandable code.
About Stancl/tenancy
This package provides automatic multi-tenancy in application. This means that, in order to implement multi-tenancy using this package, we do not need to change anything in our existing code. This package will create a separate database for each and every customer/tenant and deploy the application separately for each customer. All the tenants are managed by root customer or admin.
What would be the DB structure
This package will create 2 tables (domains and tenants) into central database. For tenant database, the migration files will be created which are located in database/migrations/tenant.
Domains Table: This table is used to store domain name, tenant unique id which is also included in the name of tenant database and connected customer id. The domains table is connected with the tenants table using tenant_id column, the tenant_id column is a foreign key for domains table and primary key for the tenants table.
Tenants Table: This table has only 2 fields first is id and second one is data. The data field is used to store all the data about customer in Json format.
For more information, visit: https://tenancyforlaravel.com/docs/v3/quickstart