F
Filamentβ€’9mo ago
Frank Wiebe

Access the current tenant in the importer resolveRecord() method

I'm using filament multitenancy. And I created an import for users. However, when I try to access the current tenant in the resolveRecord method to associate the user with the team I get Call to a member function members() on null.
public function resolveRecord(): ?User
{
$team = Filament::getTenant();

$user = User::firstOrNew([
'email' => $this->data['email'],
]);

$team->members()->associate($user->id);

return $user;
}
public function resolveRecord(): ?User
{
$team = Filament::getTenant();

$user = User::firstOrNew([
'email' => $this->data['email'],
]);

$team->members()->associate($user->id);

return $user;
}
My team model has
public function members(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
public function members(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
3 Replies
Frank Wiebe
Frank WiebeOPβ€’8mo ago
Bumping this. It doesn't look like I have access to the current tenant in the importer. Just read in the docs that only a belongsTo relationship works with the importer. But I have a many to many. Anyone know how to go about saving a many to many relationship when importing?
Diogo Pinto
Diogo Pintoβ€’8mo ago
Yes @Frank Wiebe use the hooks provided on the documentation. You have a bunch of them, and you can create relationships on the record you just created or are about to create.
James
Jamesβ€’7mo ago
You can set options on the action, which are serialized in the jobs table and can be accessed via the importer $this->options, so you can set the tenant_id there: https://discord.com/channels/883083792112300104/1179875310221660251/1235250103284138004 Would not be a good idea to get the current tenant incase the tenant switches before the job completes πŸ˜‰ P.S Hooks are of no use as they are executred by the queue worker, so wont have access to tenant either
Want results from more Discord servers?
Add your server