I'm building a web app with FilamentPHP v3 to manage Suricata configurations and potentially apply a new one or rollback to an older one.
To achieve this, I have three models, Configuration (name, description, host_id), ConfigurationVersion (configuration_id, user_id, version_number, is_active) and ConfigurationParam (param_name, param_value, param_type, is_editable).
I'm using param_type to group configuration options by functional areas to easen the work with the whole config, so param_type its an enum (general, network, performance, directories, logging, detection).
I'm trying to build the ConfigurationResource form like this:
public static function form(Form form): Form
{
paramTypes = ConfigurationParam::select('param_type')
->distinct()
->pluck('param_type', 'param_type');
Sadly, although I have a latestVersion() relation on Configuration, like this:
public function latestVersion(): HasOne
{
return this->hasOne(ConfigurationVersion::class)->latestOfMany();
}
I seem to not be able to load the latest configuration version params in the form. What am I doing wrong?
Also, I have a params() relation on ConfigurationVersion, like this:
public function params(): HasMany
{
return this->hasMany(ConfigurationParam::class, 'configuration_version_id');
}
The issue you're encountering stems from the way you're trying to access nested relationships in your form schema. The relationship() method used in Filament's form fields expects a direct relationship from the model being edited, not a nested one.
In your case, you're editing the Configuration model, and you're trying to access a nested relationship latestVersion.params. However, latestVersion is a relationship on Configuration, and params is a relationship on ConfigurationVersion, making latestVersion.params a nested relationship. The relationship() method cannot handle this, which is why your parameters aren't loading correctly.
Here's how you can resolve this issue:
Remove the ->relationship('latestVersion.params') from your Repeater: Since the relationship() method can't handle nested relationships, you'll need to handle the loading and saving of configuration_params manually.
Manually Load the Parameters in mutateFormDataBeforeFill(): Override the mutateFormDataBeforeFill() method in your ConfigurationResource to load the parameters from the latest version and populate the configuration_params field in the form.
Manually Save the Parameters in mutateFormDataBeforeSave(): Override the mutateFormDataBeforeSave() method to handle the creation of a new ConfigurationVersion and save the parameters entered in the form.
Adjust Your Form Schema: Since you're handling the loading and saving manually, you can remove the relationship() method and adjust your form fields accordingly.
Here's how you can implement these steps:
php
// In your ConfigurationResource
protected function mutateFormDataBeforeFill(array $data): array
{
$latestVersion = $this->record->latestVersion;
if ($latestVersion) {
// Load the parameters from the latest version
$data['configuration_params'] = $latestVersion->params->map(function ($param) {
return $param->only(['param_name', 'param_value', 'param_type']);
})->toArray();
} else {
$data['configuration_params'] = [];
}
return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
// Extract configuration parameters from the data
$configurationParams = $data['configuration_params'] ?? [];
unset($data['configuration_params']);
// Save or update the Configuration model
$configuration = $this->record ?: Configuration::create($data);
// Create a new ConfigurationVersion
$configurationVersion = new ConfigurationVersion([
'configuration_id' => $configuration->id,
'user_id' => auth()->id(),
'version_number' => ($configuration->versions()->max('version_number') ?? 0) + 1,
'is_active' => true,
]);
$configurationVersion->save();
// Save the new ConfigurationParams
foreach ($configurationParams as $paramData) {
$configurationVersion->params()->create($paramData);
}
// Optionally, deactivate previous versions
$configuration->versions()
->where('id', '!=', $configurationVersion->id)
->update(['is_active' => false]);
return $data;
}
Adjust your form schema to remove the relationship() method and handle the grouping of parameters if needed:
By handling the loading and saving of configuration_params manually, you're ensuring that the parameters are correctly associated with the latest configuration version. This approach also allows you to create a new ConfigurationVersion each time the parameters are updated, which seems to be part of your application's logic.
Summary: The relationship() method in Filament forms expects a direct relationship from the model, not a nested one. To fix the issue, remove ->relationship('latestVersion.params') from the Repeater, and manually handle the loading and saving of configuration_params by overriding mutateFormDataBeforeFill() and mutateFormDataBeforeSave() in your ConfigurationResource.