Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Multiple Tenants and Switching Between Them

Premium
3:42

Now, let's take care of the situation of multiple tenants per user.

We have the database structure already, with tenant_user pivot table. Now, the question is where to save the active current tenant.


Saving Current Tenant

Two choices that I would suggest:

  1. A boolean column is_active in the pivot.
  2. Saving current tenant ID in the users table.

In this lesson, we will use the second method and add the current_tenant_id column to the users table.

First, the migration.

php artisan make:migration "add current tenant id to users table"

database/migrations/xxx_add_current_tenant_id_to_users_table.php:

Schema::table('users', function (Blueprint $table) {
$table->foreignId('current_tenant_id')->nullable()->constrained('tenants');
});

app/Models/User.php:

class User extends Authenticatable
{
protected $fillable = [
'name',
'email',
'password',
'current_tenant_id',
];
 
// ...
}

Let's refresh the database so we can make sure that everything works.

php artisan migrate:fresh

Then, we need to set the active tenant when a user...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

RB
rahul barua ✓ Link copied!

hi, i am new to laravel and vue3 and have been following your program, so foar its been helpfull.

i a building a app with seperate front end and backend, so my app is essentailly based on apis, now i am stck i need to understand how do i add global scopes created in laravel into vue 3 front end

any help would be much appreciated

regards rahul

RB
rahul barua ✓ Link copied!

further my use case here is to crete workspace and all the back end resoureces to be restricted by the workspace,

need 1: alll user are in default workspace and only few have ability to create and work on a new workspace. need 2:all files and foleder in a work space are restricted to the users with ability to view/moderate the workspace

i have followed ur instructions and have a api to manager/moderate workspace but i need all models to be restricted by the workspace, only issue is reactivity at the fronend with vue

in this example you have scope defiend on the blade with works like a charm but i have not been able to replicate in vue front end which is completely separate.

Please help

PK
Povilas Korop ✓ Link copied!

Well, in theory it should work like this:

From your separate front-end, you ask the API with the user credentials, and then by that user you get the data only for that user.

So it doesn't change on the back-end, it's still the same global scope. The problem is probably how you authenticate user from the front-end.

EY
Elías Yambay ✓ Link copied!

Hi there, great job on the tutorial!

I used to approach multitenancy by storing the current tenant in a session key.

Whenever the user switched tenants, I would rewrite the session key. Is there a problem with that aproach? Maybe security issues or something else?

M
Modestas ✓ Link copied!

As long as you are checking with a middleware that the session value (tenant id or identifier) actually belongs to the user - you should be good to go

EY
Elías Yambay ✓ Link copied!

Great! thanks