Courses

Laravel 12 Multi-Tenancy: All You Need To Know

Accept Invitation: Existing User or Register New User

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Handling existing user invitation acceptance
- Managing tenant switching after acceptance
- Creating registration flow for new users
- Implementing subdomain redirection after acceptance

Video Version of the Lesson

[Only for premium members]

Link to the repository

[Only for premium members]

Let's finish the invitation system by accepting the invitation.

If I'm logged in to another tenant at the moment, I need to:

  1. Accept the invitation.
  2. Attach me to the tenant.
  3. Set the current tenant from the invitation.
  4. Redirect to the invited tenants' dashboard.

app/Http/Controllers/UserController.php:

class UserController extends Controller
{
// ...
 
public function acceptInvitation(string $token)
{
$invitation = Invitation::where('token', $token)
->whereNull('accepted_at')
->firstOrFail();
 
if (auth()->check()) {
$invitation->update(['accepted_at' => now()]);
 
auth()->user()->tenants()->attach($invitation->tenant_id);
 
auth()->user()->update(['current_tenant_id' => $invitation->tenant_id]);
 
$tenantDomain = str_replace('://', '://' . $invitation->tenant->subdomain . '.', config('app.url'));
return redirect($tenantDomain . route('dashboard', absolute: false));
} else {
// redirect to register
}
}
}

Now, if I register with a new user, this user has only its own tenant.

From another user, I send email invitations to the newly registered user.

As you can see, when the invitation is accepted, the user should be redirected to the bbb subdomain tenant and should have two tenants.

We can see that after accepting the invitation, the user is redirected to...

The full lesson is only for Premium Members.
Want to access all 21 video+text lessons of this course? (1 h 21 min)

You also get:

  • 77 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord