F
Filament16mo ago
Mikazor

Customizing the creation process of relationship

I found in docs that I can customize the creation process of resources. How to get relationship data in the handleRecordCreate method? I want to use repository pattern
Solution:
Try dd($this->data) instead
Jump to solution
5 Replies
Patrick Boivin
Patrick Boivin16mo ago
Can you share a bit more context on how your form is setup? What's the relationship, etc.
Mikazor
MikazorOP16mo ago
This is a standard User Model with Role Relation from Spatie Permissions.
// User Resource
public static function form(Form $form): Form
{
return $form->schema([
\Filament\Forms\Components\Section::make()->schema([
\Filament\Forms\Components\TextInput::make('username')
->required(),
\Filament\Forms\Components\TextInput::make('name')
->required(),
\Filament\Forms\Components\TextInput::make('email')
->email(),
\Filament\Forms\Components\TextInput::make('password')
->password()
->dehydrateStateUsing(fn(string $state): string => Hash::make($state))
->dehydrated(fn(?string $state): bool => filled($state))
->required(fn(string $operation): bool => $operation === 'create')
->columnSpan(3),
])->columns(3),

\Filament\Forms\Components\Section::make()->schema([
\Filament\Forms\Components\Select::make('roles')
->multiple()
->relationship('roles', 'name')
->searchable()
->options(Role::all()->pluck('name', 'id'))
])
]);
}
// User Resource
public static function form(Form $form): Form
{
return $form->schema([
\Filament\Forms\Components\Section::make()->schema([
\Filament\Forms\Components\TextInput::make('username')
->required(),
\Filament\Forms\Components\TextInput::make('name')
->required(),
\Filament\Forms\Components\TextInput::make('email')
->email(),
\Filament\Forms\Components\TextInput::make('password')
->password()
->dehydrateStateUsing(fn(string $state): string => Hash::make($state))
->dehydrated(fn(?string $state): bool => filled($state))
->required(fn(string $operation): bool => $operation === 'create')
->columnSpan(3),
])->columns(3),

\Filament\Forms\Components\Section::make()->schema([
\Filament\Forms\Components\Select::make('roles')
->multiple()
->relationship('roles', 'name')
->searchable()
->options(Role::all()->pluck('name', 'id'))
])
]);
}
// Edit User
protected function handleRecordUpdate(Model $record, array $data): Model
{
dd($data); // How to get updated (result of Select::make) relation here?
}
// Edit User
protected function handleRecordUpdate(Model $record, array $data): Model
{
dd($data); // How to get updated (result of Select::make) relation here?
}
array:3 [// app/Filament/Resources/UserResource/Pages/EditUser.php:29
"username" => "123"
"name" => "2345423"
"email" => null
]
array:3 [// app/Filament/Resources/UserResource/Pages/EditUser.php:29
"username" => "123"
"name" => "2345423"
"email" => null
]
Solution
Patrick Boivin
Patrick Boivin16mo ago
Try dd($this->data) instead
Mikazor
MikazorOP16mo ago
Yeah, that works. Maybe you know how to customize the delete process?
Patrick Boivin
Patrick Boivin16mo ago
The delete process is all contained in the action I think
Want results from more Discord servers?
Add your server