How to Setup Multi Tenant App in minutes with Laravel
Tags: laravel, multitenancy, laraveltenant, saas
Published: 2023-11-06 10:53:51

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
```
4. 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:

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0y33s3g9qf6at78uzmbo.png)

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


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c946bdsahi6evhkjnlzc.png)

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:


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q441lyiazu3v0avtbf1w.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8e71jgd44rn212v407qv.png)

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


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1fwe4ofi2lcnhlf7hnq.png)

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



![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouc1bstragznt8l611lk.png)

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


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3g3c5n7a76osvc3dkpqh.png)

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

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmygkvmruy4aon8f5zob.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfzkocw36kifl5bf7ez0.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujhkfvibi7lxui2wdd30.png)

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

** updated web.php**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7836x4xa81axo6uriqw.png)

**updated tenant.php**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6rc7hk67kr1dasvo6lw.png)