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