F
Filamentβ€’2y 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β€’2y ago
Message Not Public
Sign In & Join Server To View
Dennis Koch
Dennis Kochβ€’2y ago
Crylar
CrylarOPβ€’2y 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β€’2y ago
Why do you need the menu_id in the form?
Crylar
CrylarOPβ€’2y 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
CrylarOPβ€’2y 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
CrylarOPβ€’2y ago
form->fill pass menu_id in my example. I have looked at how EditPage does it. hidden field gets populated though πŸ™‚
Crylar
CrylarOPβ€’2y ago
so this place receives data through fill but relationship is somehow not triggered
Dennis Koch
Dennis Kochβ€’2y 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
CrylarOPβ€’2y ago
This is not getting triggered on a create page load but not found what prevents it yet. :/
Dennis Koch
Dennis Kochβ€’2y ago
Probably because there is usually no state on create You could try adding data inside mount()
Crylar
CrylarOPβ€’2y ago
yeh, I was following the hydrateState on the components right now and that's where it gets skipped
Crylar
CrylarOPβ€’2y ago
yeh so basically it gets cut off here because my dummy model is market as non existant
Want results from more Discord servers?
Add your server