How do you get the current tenant id?

I'd like to get the current team id (team = my tenant) when in a controller, e.g.

<?php

namespace App\Http\Controllers;

use App\Models\Team;
use Filament\Facades\Filament;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Log;

class AirtableOAuthController extends Controller
{
    public function connect(Request $request)
    {
        $tenant = Filament::getTenant(Team::class);
        dd($tenant);
    }
}


But it's returning "null" (also returns null if I just do Filament::getTenant();
Solution
Had to go digging in the filament vendor package... Basically, you add an event listener to the setTenant function.

php artisan make:listener UpdateLatestTeamId --event=TenantSet

Then in your new file (e.g. app/Listeners/UpdateLatestTeamId.php) you could put

<?php

namespace App\Listeners;

use Filament\Events\TenantSet;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class UpdateLatestTeamId
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
        Log::info('UpdateLatestTeamId::__construct()');
    }

    /**
     * Handle the event.
     */
    public function handle(TenantSet $event): void
    {
        // get the user from the event
        $user = $event->getUser();

        // get the tenant (team) from the event
        $team = $event->getTenant();

        // update the user's latest_team_id
        $user->latest_team_id = $team->id;
        $user->save();
    }
}


And then you need to register your new listener in app/Providers/EventServiceProvider.php like so

    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'Filament\Events\TenantSet' => [
            'App\Listeners\UpdateLatestTeamId',
        ],
    ];
Was this page helpful?