Skip to main content

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

Read more here

Tenant Owner: Manage Users - Menu and Permission

Premium
2:44

In a few upcoming lessons, let's work on the invitation system.

So, I've just registered, and I want to invite new users to my team/tenant. For that, we will create a new menu item on top called Users where we will manage the invitations and the users who are invited to the team.

For that, we must introduce a concept called "Team Owner" or the person who created the tenant. Again, there are multiple ways how you can do that in the database. We will add an is_owner boolean column to the tenant_user pivot table, which will default to false and set to true during registration.


Database Changes

So, first, let's add a column titled is_owner.

php artisan make:migration "add is owner to tenant user table"

database/migrations/xxx_add_is_owner_to_tenant_user_table.php:

Schema::table('tenant_user', function (Blueprint $table) {
$table->boolean('is_owner')->default(false);
});

app/Models/Tenant.php:

class Tenant extends Model
{
// ...
 
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
return $this->belongsToMany(User::class)->withPivot('is_owner');
}
}

app/Models/User.php:

class User extends Authenticatable
{
// ...
 
public function tenants(): BelongsToMany
{
return $this->belongsToMany(Tenant::class);
return $this->belongsToMany(Tenant::class)->withPivot('is_owner');
}
}

Registration: Setting the Owner

Next, when we attach a user to a tenant...

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

G
Girish ✓ Link copied!

I have run complete repository but showing 419 Page Expired. Is there anything missing from my side?

PK
Povilas Korop ✓ Link copied!

419 page expired usually means something is wrong with your session storage so it doesn't save session cookies as usual. Sorry hard to "blindly" debug it to tell you what can be wrong.

BA
Bruno Alfred ✓ Link copied!

How Can I implement this concept of Multitenancy on the Login and Register Routes where the user is not yet exisiting.

If you apply globalscope and let model self::creating populate the db with user()->id, when accessing the login and register routes Laravel seems to fire the Memory overflow error.

PHP Fatal error : Allowed memory size of xxxxxx bytes executed (tried to allocated xxxx bytes) in vendor/laravel/framework/src/Illuminate/Database/Eloquent/builder.php on line 288

PHP Fatal error : Allowed memory size of xxxxxx bytes executed (tried to allocated xxxx bytes) in vendor/laravel/framework/src/Illuminate/Database/Eloquent/DatabaseManager.php on line 91

PK
Povilas Korop ✓ Link copied!

In the global scope, the best way is to probably check if (auth()->check()) before applying the scope.

UP
Unisoft Prima ✓ Link copied!

Hi Povilas, I think you should add current_tenant_id when define Gate 'manage_users'?

Gate::define('manage_users', function(User $user) { $tenantId = $user->current_tenant_id; return $user->tenants()->where('id', $tenantId)->wherePivot('is_owner', true)->exists(); });

PK
Povilas Korop ✓ Link copied!

You're probably right, good catch!