F
Filamentβ€’16mo ago
Crylar

Filling default value in forms via relationship

So lets say I have Categories that have a single Menu. Is there a way to create a dummy record with menu_id pre-defined, so when I am creating it would just fetch based on a defined menu_id and fill a title from the Menu
public static function form(Form $form): Form
{
$defaultMenuId = Route::current()->parameter('menu');

return $form->schema([
Forms\Components\Card::make([
Forms\Components\Grid::make()
->relationship('menu')
->columns(0)
->schema([
Forms\Components\TextInput::make('title')
->dehydrated(false) // prevent saving to database
->label('Menu')
->disabled()
->required()
]),
Forms\Components\Hidden::make('menu_id')
->default($defaultMenuId),
public static function form(Form $form): Form
{
$defaultMenuId = Route::current()->parameter('menu');

return $form->schema([
Forms\Components\Card::make([
Forms\Components\Grid::make()
->relationship('menu')
->columns(0)
->schema([
Forms\Components\TextInput::make('title')
->dehydrated(false) // prevent saving to database
->label('Menu')
->disabled()
->required()
]),
Forms\Components\Hidden::make('menu_id')
->default($defaultMenuId),
In a regular Laravel I could just:
$category = Category::make(['menu_id' => $defaultMenuId]);
$category->menu->title; // and reach title without category created in the database
$category = Category::make(['menu_id' => $defaultMenuId]);
$category->menu->title; // and reach title without category created in the database
How would it be possible to achieve a similar logic in a filament? Thanks.
13 Replies
Unknown User
Unknown Userβ€’16mo ago
Message Not Public
Sign In & Join Server To View
Dennis Koch
Dennis Kochβ€’16mo ago
Crylar
Crylarβ€’16mo ago
Thanks. I think it's after user clicks "Create" button and data is about to get inserted into database but I need something where I could offer a dummy model with just menu_id before form is filled in CreatePage
Dennis Koch
Dennis Kochβ€’16mo ago
Why do you need the menu_id in the form?
Crylar
Crylarβ€’16mo ago
I want to display disabled value as reference for the user, so he would be well informed for what menu category is getting created. πŸ™‚ Hidden field in my example acts as relation ID and TextInput with dehydrate(true) is for user information.
Crylar
Crylarβ€’16mo ago
so I was trying with fillForm now but something is still missing. This is called before form is loaded so I guess it's right spot to override.
Crylar
Crylarβ€’16mo ago
form->fill pass menu_id in my example. I have looked at how EditPage does it. hidden field gets populated though πŸ™‚
Crylar
Crylarβ€’16mo ago
so this place receives data through fill but relationship is somehow not triggered
Dennis Koch
Dennis Kochβ€’16mo ago
Ah, you tried to prefill the relation by setting a menu_id field. That doesn't work. Not sure what's the best approach to prefill Layout relationships
Crylar
Crylarβ€’16mo ago
This is not getting triggered on a create page load but not found what prevents it yet. :/
Dennis Koch
Dennis Kochβ€’16mo ago
Probably because there is usually no state on create You could try adding data inside mount()
Crylar
Crylarβ€’16mo ago
yeh, I was following the hydrateState on the components right now and that's where it gets skipped
Crylar
Crylarβ€’16mo ago
yeh so basically it gets cut off here because my dummy model is market as non existant