F
Filament2mo ago
erwin

Issue displaying & editing pivot field (role) in multi-tenant UserResource

Hi, I’m working on a multi-tenant Laravel Filament app with companies and users in a many-to-many relationship. The pivot table is company_user and contains a role field. I’ve set up my UserResource like this: protected static ?string $tenantOwnershipRelationshipName = 'companies'; This correctly filters users to the current tenant. In my table, I want to show name, email, and role:
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('email'),
Tables\Columns\TextColumn::make('role'),
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('email'),
Tables\Columns\TextColumn::make('role'),
name and email display fine, but role does not show. I expected it to work like how relationship managers handle pivot fields. Models User model:
public function companies(): BelongsToMany
{
return $this->belongsToMany(Company::class)
->withPivot(['role'])
->withTimestamps();
}
public function companies(): BelongsToMany
{
return $this->belongsToMany(Company::class)
->withPivot(['role'])
->withTimestamps();
}
Company model:
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class)
->withPivot(['role'])
->withTimestamps();
}
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class)
->withPivot(['role'])
->withTimestamps();
}
Form fields in UserResource:
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),

Forms\Components\TextInput::make('email')
->email()
->required()
->maxLength(255),

Forms\Components\Select::make('role')
->required()
->default(CompanyRole::Admin)
->options(CompanyRole::class),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),

Forms\Components\TextInput::make('email')
->email()
->required()
->maxLength(255),

Forms\Components\Select::make('role')
->required()
->default(CompanyRole::Admin)
->options(CompanyRole::class),
The problems I’m facing: 1. In the table, role does not display. 2. When creating a user, Filament inserts data into the pivot table but ignores the value from the role select — it always uses the DB default (member). 3. When editing a user, the form loads name and email, but does not load the current role from the pivot table. How can I correctly display, load, and save a pivot field (role) in this UserResource with multi-tenancy enabled?
2 Replies
erwin
erwinOP2mo ago
I couldn’t figure out a clean solution, so I ended up moving Users management from a dedicated resource into the Company resource (the tenant in my case) using a relation manager. Inside the Company resource, I now display the related users so they can be managed directly. The previous UX felt awkward, users had to click Companies (which only showed the current tenant), open its details, and only then manage the users. Instead, I added a user management section directly to the Tenant Profile page. Now, when a user visits tenant settings, they see the tenant’s info and, right below it, the related users. This feels much more natural and streamlined. I found the way to implement it here: https://www.answeroverflow.com/m/1250377468712583220
Add Relation Manager to EditTenantProfile - Filament
How can I add a RelationManager to the EditTenantProfil page? I tried adding getRelations() to the EditTeamProfile, but it doesn't show up. I figure something about a custom view, but I am not that experienced in custom views? I have a TeamResource with Create/Edit, where the UserRelationManager works perfectly, but not on the tenant edit prof...
Tieme
Tieme2mo ago
i solved this by creating a model for my pivot to https://laravel.com/docs/12.x/eloquent-relationships#defining-custom-intermediate-table-models and a seperate resource for my pivot model. Below my implementation Pivot model
class CompanyUser extends Pivot
{
protected $table = 'company_user';

protected $fillable = [
'company_id',
'user_id',
'status',
'unsubscribe_at',
];

protected $casts = [
'status' => CustomerStatus::class,
'unsubscribe_at' => 'datetime',
];

public function company(): BelongsTo
{
return $this->belongsTo(Company::class);
}

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
class CompanyUser extends Pivot
{
protected $table = 'company_user';

protected $fillable = [
'company_id',
'user_id',
'status',
'unsubscribe_at',
];

protected $casts = [
'status' => CustomerStatus::class,
'unsubscribe_at' => 'datetime',
];

public function company(): BelongsTo
{
return $this->belongsTo(Company::class);
}

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
Companies model
public function customers(): belongsToMany
{
return $this->belongsToMany(User::class, 'company_user')
->belongsToMany(\App\Models\User\User::class)
->using(CompanyUser::class)
->withPivot('status', 'unsubscribe_at')
->withTimestamps();
}
public function customers(): belongsToMany
{
return $this->belongsToMany(User::class, 'company_user')
->belongsToMany(\App\Models\User\User::class)
->using(CompanyUser::class)
->withPivot('status', 'unsubscribe_at')
->withTimestamps();
}
User modal
public function companies(): belongsToMany
{
return $this->belongsToMany(Company::class, 'company_user')
->using(\App\Models\Owner\CompanyUser::class)
->withPivot('status', 'unsubscribe_at')
->withTimestamps();
}
public function companies(): belongsToMany
{
return $this->belongsToMany(Company::class, 'company_user')
->using(\App\Models\Owner\CompanyUser::class)
->withPivot('status', 'unsubscribe_at')
->withTimestamps();
}
Eloquent: Relationships - Laravel 12.x - The PHP Framework For Web ...
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.

Did you find this page helpful?