Skip to content

Filament: Get the default Tenant for the User model

Blog  ✺  Filament  ✺  Laravel  ✺  Code Snippet

Ever needed to get the default tenant for a user in your multi-tenant Filament app? I know I did and as (almost) always Filament got a solution ready for you.

In my case currentTeam and allTeams() both come from Laravel Jetstream. When I started building the app, it used Livewire 3 and Laravel Jetstream for the multi-tenancy. If you have a similar concept and somehow store the current tenant for the user, you can quickly adjust the code to your liking.

Implement the HasDefaultTenant interface on the User model

Filament provides a HasDefaultTenant interface that you can simply implement on your User model, in my case like this 👇

Why is this useful?

Normally when you sign into your Filament panel the user will be redirected to the first tenant that is returned from the getTenants() method. This might be good for some use cases, but in most cases it makes more sense to go back to the last tenant the user was in.

Learn more in the Filament docs

The documentation for Filament is getting better with every release. In the case of getDefaultTenant, they do have even more on the topic. Go check it out:

Multi-tenancy - Panel Builder - Filament
Before you go on about your day. Please let me know in the comments or on X if you find these small tips useful? I'm planning on writing more as I learn more about Filament.

Comments

You might like

Add Dimensions to Image and Video Uploads in Laravel

Laravel

Calm: Peaceful Pictures

Opinion  ✺  Photography