Typhon
Typhon
FFilament
Created by Typhon on 4/26/2024 in #❓┊help
Roles on pivot table
@toeknee I thought the teams feature was supposed to be used for a multi-tenant setup?
8 replies
FFilament
Created by Typhon on 4/26/2024 in #❓┊help
Roles on pivot table
@toeknee roles are on the pivot table between Users and Organisations so that users are can only do certain stuff within that organisation. This is different from having a role on User.
8 replies
FFilament
Created by Typhon on 4/26/2024 in #❓┊help
Roles on pivot table
Creating your own edit action and reproducing relationship functionality works but is quite ugly:
public function editRoleAction()
{
return Tables\Actions\EditAction::make()
->mutateRecordDataUsing(function ($record, $data) {
$data['organisation_roles'] = $record->pivot->roles->pluck('id')->toArray();
return $data;
})
->modalWidth(MaxWidth::Large)
->using(function ($record, array $data) {
$roles = collect($data['organisation_roles'])->map(function ($role) {
return Role::where('id', $role)->firstOrFail()->name;
})->toArray();
$record->pivot->syncRoles($roles);
$record->update($data);

return $record;
})
->form([
Select::make("organisation_roles")
->required()
->options(
// Only get roles starting with 'psi'
Role::where('name', 'like', 'psi_%')
->pluck('name', 'id')
->mapWithKeys(fn($item, $key) => [$key => __('roles.' . $item)])
->toArray()
)
->label(__("labels.roles"))
->multiple()
->preload()
]);
}
public function editRoleAction()
{
return Tables\Actions\EditAction::make()
->mutateRecordDataUsing(function ($record, $data) {
$data['organisation_roles'] = $record->pivot->roles->pluck('id')->toArray();
return $data;
})
->modalWidth(MaxWidth::Large)
->using(function ($record, array $data) {
$roles = collect($data['organisation_roles'])->map(function ($role) {
return Role::where('id', $role)->firstOrFail()->name;
})->toArray();
$record->pivot->syncRoles($roles);
$record->update($data);

return $record;
})
->form([
Select::make("organisation_roles")
->required()
->options(
// Only get roles starting with 'psi'
Role::where('name', 'like', 'psi_%')
->pluck('name', 'id')
->mapWithKeys(fn($item, $key) => [$key => __('roles.' . $item)])
->toArray()
)
->label(__("labels.roles"))
->multiple()
->preload()
]);
}
8 replies
FFilament
Created by Typhon on 4/26/2024 in #❓┊help
Roles on pivot table
Tables\Columns\TextColumn::make("pivot.roles")
Tables\Columns\TextColumn::make("pivot.roles")
Does print out the roles json but pivot.roles.name returns nothing
8 replies