Tenancy: associate created record with tenant in CreateAction

Hello, How can we efficiently link a tenant to a record when it's being generated via a CreateAction, especially within the relationship manager context? Currently, if someone initiates a new resource item within the relationship manager, this item doesn't get linked with their respective account. Please note, the subsequent code snippet from handleRecordCreation isn't being executed:
if ($tenant = Filament::getTenant()) {
return $this->associateRecordWithTenant($record, $tenant);
}
if ($tenant = Filament::getTenant()) {
return $this->associateRecordWithTenant($record, $tenant);
}
Solution:
Thanks for the reply, I'm adding this comment to help users with the same problem. Since my model is already very complicated I didn't want to add a trait to it. What I did is I used the after hook. ```php...
Jump to solution
3 Replies
Disouric
Disouric13mo ago
Bumping this one ^
Brian Kidd
Brian Kidd13mo ago
Because, according to the docs, Filament tenancy is limited to the current resource list, edit, and assigning the tenant ID to the primary resource, I’ve adapted this trait to use in my Filament project. I’ve used it in other codebases and I’ve never had an issue. Others may have better solutions … php trait BelongsToTeam { public static function bootBelongsToTeam() { switch (Filament::getPanel()?->getId()) { case 'app': static::addGlobalScope(new TeamScope()); default: // do nothing } if ($teamId = Filament::getTenant()?->id) { static::creating(function (Model $model) use ($teamId) { $model->team_id = $teamId; }); } } public function team(): BelongsTo { return $this->belongsTo(Team::class); } }
Solution
Disouric
Disouric11mo ago
Thanks for the reply, I'm adding this comment to help users with the same problem. Since my model is already very complicated I didn't want to add a trait to it. What I did is I used the after hook.
use Filament\Facades\Filament;

Tables\Actions\CreateAction::make()
->after(function ($record) {
if (Filament::getTenant() !== null) {
$record->teams()->attach(Filament::getTenant()->id, ['role' => 'administrator']);
}
}),
use Filament\Facades\Filament;

Tables\Actions\CreateAction::make()
->after(function ($record) {
if (Filament::getTenant() !== null) {
$record->teams()->attach(Filament::getTenant()->id, ['role' => 'administrator']);
}
}),
Want results from more Discord servers?
Add your server