FilamentF
Filament7mo ago
1 reply
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*/)   
    ])
    //[...]
    ;
}


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?
Was this page helpful?