F
Filamentβ€’12mo ago
Saifallak

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
Patrick Boivinβ€’12mo ago
Probably in a page hook, like afterCreate()?
Saifallak
Saifallakβ€’12mo 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
Patrick Boivinβ€’12mo ago
I'm not sure actually, there may be an automated way. I have not yet tried a multi-tenant project with Filament πŸ˜„
Saifallak
Saifallakβ€’12mo ago
me too, still trying it out.
Saade
Saadeβ€’12mo 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
Saifallakβ€’12mo 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
Saadeβ€’12mo ago
show me an example where it does not work resource + tenant model + model