create record when using tenancy

Does filament auto associate created model to current tenant? .. if not how would i do that?
8 Replies
Patrick Boivin
Probably in a page hook, like afterCreate()?
Saifallak
SaifallakOP2y ago
I though it will auto associate it, since it filters it in first place, I asked because idk if its a bug or intended. Anyway afrer create looks great idea
Patrick Boivin
I'm not sure actually, there may be an automated way. I have not yet tried a multi-tenant project with Filament 😄
Saifallak
SaifallakOP2y ago
me too, still trying it out.
Saade
Saade2y ago
yes, it does automatically associate the record with the tenant. In your tenant model, you should define the relation between your resource model and your tenant model. for example:
// Your tenant class
class Team extends Model {

public function users(): HasMany
{
return $this->hasMany(User::class);
}
}


// Your resource model class
class User extends Model {

public function tenant(): BelongsTo
{
return $this->belongsTo(Team::class);
}
}
// Your tenant class
class Team extends Model {

public function users(): HasMany
{
return $this->hasMany(User::class);
}
}


// Your resource model class
class User extends Model {

public function tenant(): BelongsTo
{
return $this->belongsTo(Team::class);
}
}
then Filament will be able to automatically associate the model with the tenant model
Saifallak
SaifallakOP2y ago
nope, it didn't with me, had to use aftercreate method @saadeguilherme after more digging , i see sometimes it works and sometimes not, idk why, i will dig more.
Saade
Saade2y ago
show me an example where it does not work resource + tenant model + model

Did you find this page helpful?