Courses

Laravel 11 Multi-Tenancy: All You Need To Know

Tenant Owner: Manage Users - Menu and Permission

Summary of this lesson:
- Adding is_owner boolean to tenant_user pivot table
- Setting owner status during user registration
- Creating Users menu for tenant management
- Implementing Gate authorization for user management

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 22 lessons of this course? (88 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord