How to Setup Multi Tenant App in minutes with Laravel

https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmziug7lqvfqzrtsetaa6.png

In this article you will learn how to setup the basic Laravel multi tenant app in minutes with some simple steps.

Multi tenancy is a good concepts for new SaaS based Applications it will help you to keep the one codebase for multiple domains and each domain contains its own data.

Let get Started!

  1. Create a new Laravel Project
  2. Install/setup Laravel Breeze
  3. Install multitenancy package with following command
composer require stancl/tenancy
  1. After it completes run the following command
php artisan tenancy:install

It will create:

migrations
a config file (config/tenancy.php),
a routes file (routes/tenant.php),
and a service provider file
app/Providers/TenancyServiceProvider.php

Recap
What we have done so far
a. create a new project
b. install tenancy package

Step for multiple domains and database

Once the above steps are done we need to register our tenancy service provider in the following file
Then add the service provider to your config/app.php file:

Now we need to create a Tenant Model for that we will use following command:

php artisan make:model Tenant

After that a model will Created in side the App/Models/ we need to change the code with following later we will update it more according to our needs

by default it will use the package Tenant Model but we want the system to use our model that we have just created for that we have to go in the config folder/directory and then find the tenancy.php file and make the following changes

'tenant_model' => Tenant::class,

change the Above line of code with

'tenant_model' => \App\Models\Tenant::class,

If you don’t need domains or databases, ignore the steps above. Everything will work just as well.

You can create tenants like any other models:

You may register central routes in routes/web.php or routes/api.php like you’re used to. However, you need to make one small change to your RouteServiceProvider.
You don’t want central routes — think landing pages and sign up forms — to be accessible on tenant domains. For that reason, register them in such a way that they’re only accessible on your central domains.Now we need to set the routes for the tenants and central app
let go the the RouteServiceProvider.php and replace the code with following code from boot function

Image description

Tenant routes
You may register tenant routes in routes/tenant.php file
by default it will look like this

Now we need to create copy the migration files from migration folder to the tenant folder that resides in migration folder and copy the required migration files

once this done we need to create a job that will insert the data in tenant table and also generate the tenant database and insert that record in it

php artisan make:job SeedTenantJob

and replace the file code with the code bellow

what this file do is it will always run as soon as you will hit the create tenant function and once it will be execute it will run the migration and insert the first record for you so you can login and access your tenant application

but still we have to do some more fix’s to get the working tenant application

now we need to do some modifications in TenantServiceProvider.php
do the following changes

uncomment the JobSeedDatabase and add our created job bellow that \App\Jobs\SeedTenantJob::class

like mentioned in the picture. Remember that order is very important in this file if you put our job at top and JobSeedDatabase bellow that it will throw error as you need to first run the migrations then insert the data our job part is for the insertion of data and JobSeedDatabase is to run the migrations

Now your first multitenancy app is ready to go live and you can test on local but for that you need to create a TenantController from where you can register the tenants

Image description

this will be basic code you can modify it for more stuff as per your requirements

now you will need to modify your route service provider if you havent set it accordingly

Image description

now you can set you web.php and tenant.php file accordingly

** updated web.php**
Image description

updated tenant.php

Image description

Laravel News Links