How to handle selected relations during creating a record in handleRecordCreation

When I try to create a new User from this form I don't see the selected relations as ID's in the handleRecordCreation so that I can manually attach anything needed. This is how I load them
Forms\Components\Select::make('roles')
->relationship('roles', 'name')
->multiple()
->preload()
->searchable(),
Forms\Components\Select::make('permissions')
->relationship('permissions', 'name')
->multiple()
->preload()
->searchable()
Forms\Components\Select::make('roles')
->relationship('roles', 'name')
->multiple()
->preload()
->searchable(),
Forms\Components\Select::make('permissions')
->relationship('permissions', 'name')
->multiple()
->preload()
->searchable()
If I use the options instead of relationship and load them in manually I get the needed data inside of the handleRecordCreation method but is that the only way to do it?
No description
No description
Solution:
I did end up using the following ``` private static function buildRolesField(): Select {...
Jump to solution
11 Replies
toeknee
toeknee4w ago
if you use relationship then they are automatically created. Please explain in more detail what you are trying to do?
Melomancheto
MelomanchetoOP4w ago
I'm using teams and laravel shield is not supporting them, so when I try to enter a new record in the user table with permissions I get the error from the screenshot. This is why I want to handle the permission creation manually. Thank you
No description
toeknee
toeknee4w ago
So you need to read the relationship docs 😉 you need to mutation the relationship save method
Melomancheto
MelomanchetoOP4w ago
I was looking at the selection docs 😄 but I guess I need to look there too, thanks I will go and have a look at it
toeknee
toeknee4w ago
->mutateRelationshipDataBeforeCreateUsing(function ($record, $data) {
$data['organization_id'] = Filament::getTenant()->id;
return $data;

})
->mutateRelationshipDataBeforeCreateUsing(function ($record, $data) {
$data['organization_id'] = Filament::getTenant()->id;
return $data;

})
Melomancheto
MelomanchetoOP4w ago
return $form
->schema([
Forms\Components\Fieldset::make('User')
->schema([
self::buildNameField(),
self::buildEmailField(),
self::buildPasswordField(),
self::buildOrganizationsFields(),
]),
Forms\Components\Fieldset::make('Permissions')
->schema([
self::buildRolesField(),
self::buildPermissionsField(),
])->mutateRelationshipDataBeforeCreateUsing(function ($record, $data) {
$data['organization_id'] = Filament::getTenant()->id;
return $data;

})
]);
return $form
->schema([
Forms\Components\Fieldset::make('User')
->schema([
self::buildNameField(),
self::buildEmailField(),
self::buildPasswordField(),
self::buildOrganizationsFields(),
]),
Forms\Components\Fieldset::make('Permissions')
->schema([
self::buildRolesField(),
self::buildPermissionsField(),
])->mutateRelationshipDataBeforeCreateUsing(function ($record, $data) {
$data['organization_id'] = Filament::getTenant()->id;
return $data;

})
]);
Since you are here I can see that I need to attach this after the schema, but I get the same error again. https://filamentphp.com/docs/3.x/forms/fields/repeater#mutating-related-item-data-before-creating It's not hitting the callback, it throws an error before that.
LeandroFerreira
shouldn't you use mutateRelationshipDataBeforeCreateUsing in the Roles/Permissions fields? If you add ->dehydrated(true) to the fields, they will be available in the $data array
Melomancheto
MelomanchetoOP4w ago
I will try the dehydrate tomorrow, thank you I've tried both but none of them worked.
LeandroFerreira
share the whole code that you are using
Melomancheto
MelomanchetoOP4w ago
This is the full UserResource @Leandro Ferreira, thank you
Solution
Melomancheto
Melomancheto3w ago
I did end up using the following
private static function buildRolesField(): Select
{
return Select::make('roles')
->relationship(
name: 'roles',
titleAttribute: 'name',
modifyQueryUsing: Utils::modifyQueryUsingCheckForAdmin(
auth()->user(),
fn (Builder $query) => $query->where('roles.' . config('permission.column_names.team_foreign_key'), Filament::getTenant()->id)
)
)
->saveRelationshipsUsing(fn (Model $record, $state, Forms\Get $get) => $record->roles()->syncWithPivotValues($state, [config('permission.column_names.team_foreign_key') => $get(config('permission.column_names.team_foreign_key'))]))
->multiple()
->preload()
->searchable();
}
private static function buildRolesField(): Select
{
return Select::make('roles')
->relationship(
name: 'roles',
titleAttribute: 'name',
modifyQueryUsing: Utils::modifyQueryUsingCheckForAdmin(
auth()->user(),
fn (Builder $query) => $query->where('roles.' . config('permission.column_names.team_foreign_key'), Filament::getTenant()->id)
)
)
->saveRelationshipsUsing(fn (Model $record, $state, Forms\Get $get) => $record->roles()->syncWithPivotValues($state, [config('permission.column_names.team_foreign_key') => $get(config('permission.column_names.team_foreign_key'))]))
->multiple()
->preload()
->searchable();
}
Want results from more Discord servers?
Add your server