F
Filament4mo ago
Mombei

Pre-fetching an Eloquent model from a relationship Select to use in Closures

Hello, In my Resource, I have a Form with a Select component, roughly as such:
use Filament\Forms\Components;

//[omitted for brevity...]

public static funciton form(Form $form): Form {
return $form
->schema([
/* [other components...] */
Components\Select::make('related_model_id')
->relationship('relationship_name', 'titleAttribute'),
Components\TextInput::make('some_field')
->afterStateUpdated(fn(/* what to put here?*/) => $somehow_related_model_instance->someMethod() /*or property, or whatever*/)
])
//[...]
;
}
use Filament\Forms\Components;

//[omitted for brevity...]

public static funciton form(Form $form): Form {
return $form
->schema([
/* [other components...] */
Components\Select::make('related_model_id')
->relationship('relationship_name', 'titleAttribute'),
Components\TextInput::make('some_field')
->afterStateUpdated(fn(/* what to put here?*/) => $somehow_related_model_instance->someMethod() /*or property, or whatever*/)
])
//[...]
;
}
I know that I can call RelatedModel::find() with the id to fetch it in the closure, but I was wondering if there was a way to pre? eager? load the selected related model instance so that afterStateUpdated() doesn't have to?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?