F
Filament•2y ago
Crylar

What is a proper way to pass additional parameters into Hidden element from the ownerRecord?

I have a self referencing Category model (id, parent_id). All of the Categories and Sub-Categories belong to the single Menu model. I am using RelationManager to solve the self referencing relationship but not sure how to pass menu_id from the ownerRecord during creation of a sub-category. Here is how my RelationManager looks like.
class ChildrenRelationManager extends RelationManager
{
protected static string $relationship = 'children';

protected static ?string $recordTitleAttribute = 'title';

protected static ?string $title = 'Sub-Categories';

protected static ?string $modelLabel = 'category';

public static function form(Form $form): Form
{
return $form->schema([
Forms\Components\Hidden::make('menu_id'), // <--- need to pass menu_id from the ownerRecord here

TranslatableKeyValue::make('title')
->languages(['lt', 'en'])
->required()
->autofocus(),

Forms\Components\FileUpload::make('image')
->nullable()
->image()
->directory('categories')
->acceptedFileTypes(['image/png', 'image/jpeg']),
])->columns(1);
}
class ChildrenRelationManager extends RelationManager
{
protected static string $relationship = 'children';

protected static ?string $recordTitleAttribute = 'title';

protected static ?string $title = 'Sub-Categories';

protected static ?string $modelLabel = 'category';

public static function form(Form $form): Form
{
return $form->schema([
Forms\Components\Hidden::make('menu_id'), // <--- need to pass menu_id from the ownerRecord here

TranslatableKeyValue::make('title')
->languages(['lt', 'en'])
->required()
->autofocus(),

Forms\Components\FileUpload::make('image')
->nullable()
->image()
->directory('categories')
->acceptedFileTypes(['image/png', 'image/jpeg']),
])->columns(1);
}
2 Replies
Crylar
CrylarOP•2y ago
I have ended up with this but not sure if there is a better way to achieve the same. 🙂
Forms\Components\Hidden::make('menu_id')
->afterStateHydrated(function (RelationManager $livewire, Forms\Components\Hidden $component) {
$component->state($livewire->ownerRecord->menu_id);
}),
Forms\Components\Hidden::make('menu_id')
->afterStateHydrated(function (RelationManager $livewire, Forms\Components\Hidden $component) {
$component->state($livewire->ownerRecord->menu_id);
}),
Dennis Koch
Dennis Koch•2y ago
You probably shouldn’t use hidden but a lifecycle hook if this shouldn’t be manipulated by the user
Want results from more Discord servers?
Add your server