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:
Solution:Jump to 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...3 Replies
Bumping this one ^
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
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.