F
Filament16mo ago
Scott

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);
}
}
<?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...
Jump to solution
2 Replies
Scott
ScottOP16mo ago
The docs talk about getting the latest tenant: https://filamentphp.com/docs/3.x/panels/tenancy#setting-the-default-tenant I've added a migration to add the latest_tenant_id column, but how do we set this value?
Solution
Scott
Scott16mo ago
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();
}
}
<?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',
],
];
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'Filament\Events\TenantSet' => [
'App\Listeners\UpdateLatestTeamId',
],
];
Want results from more Discord servers?
Add your server