F
Filament14mo ago
mxc

Resource Edit Form Not Populating Values - Model Field Names Mapped to Table Column Names

Hi all, I have to build a front-end on an existing database with horrendous and inconsistent naming of column names. I have mapped sensible names in the Model to the table column names with a bunch of get/set attribute methods. For example
public function setCompanyNameAttribute($company_name){
$this->attributes["Company Name"] = $company_name;
}
public function setCompanyNameAttribute($company_name){
$this->attributes["Company Name"] = $company_name;
}
When using plain blade templates I need to either map the form element ids to the exact column name or, in the controller, merge the request paramters with the database column names to get eloquent queries to work. When a model instance is used it all works fine and no extra work is needed. I now want to use filament forms to be able to list/edit/create etc the model objects. I can get the table method in my Resource to return and display the right columns using the model property names. Table is quite good as it can even use the database column names and still work. The issue I have is editing a row/model instance. The form displays but the existing values are not populated. If I enter data and save, the changes are saved. So far I have just replaced all values as the fields are required. I have searched, and queries chatgpt but can't find a way to be able to map the form fields and model properties to get the fields to populate the old values. Any ideas, pointers or solutions? Thanks
1 Reply
mxc
mxcOP14mo ago
I figured out I can override the html element names/ids in the mutateFormDataBeforeSave method on my EditCustomer.php page. class EditCustomer extends EditRecord
protected function mutateFormDataBeforeSave(array $data): array
{
if (array_key_exists('Company Name', $data)) {
$data['company_name'] = $data['Company Name'];
unset($data['Company Name']);
}
.....
return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
if (array_key_exists('Company Name', $data)) {
$data['company_name'] = $data['Company Name'];
unset($data['Company Name']);
}
.....
return $data;
}
Jus tto add for the creation of a new record you need to implement protected function mutateFormDataBeforeCreate(array $data): array in the cooresponding CreateRecord class.
Want results from more Discord servers?
Add your server