It is not possible to use a variable inside the make() of a FileUpload field ?

I try to make a dynamic field for a file but I get an error when I don't put a string directly inside the make() error : No synthesizer found for key: "" field : Forms\Components\FileUpload::make($eye."_pdf") ->name("{$eye}_pdf") ->label('Fichier PDF') ->directory('aic-surgery') ->hidden(fn ($get) => !self::isFieldVisible($eye, 'pdf', $get)), and $eye = 'left' or 'right'
5 Replies
drsdre
drsdre5d ago
With the 'make()' function your specify in which DB field the file reference for the upload is stored. That is not the file name itself. What I think you want to do is rename the files after the user uploaded them. Use the FileUpload component function 'getUploadedFileNameForStorageUsing()' for this. See https://filamentphp.com/docs/3.x/forms/fields/file-upload#generating-custom-file-names
Nashoba
NashobaOP5d ago
not really, here is my entire file :
Nashoba
NashobaOP5d ago
the thing is I try to make a dynamic form for this cause I need it twice,it works very well for my other fields with this in CreatePatient :
protected function mutateFormDataBeforeCreate(array $data): array
{
$leftEyeSurgery = new EyeSurgery();
$leftEyeSurgery->surgery_type_id = SurgeryType::query()->where('id', $data['left_eye_surgery_type_id'])->first()->id;
$leftEyeFields = new SurgeryField();
foreach ($data as $key => $value) {
if (str_starts_with($key, 'left_') && $key !== 'left_eye_surgery_type_id') {
$cleanKey = str_replace('left_', '', $key);
$leftEyeFields->$cleanKey = $value;
}
}
$leftEyeFields->save();
$leftEyeSurgery->surgery_fields_id = $leftEyeFields->id;
$leftEyeSurgery->save();

$rightEyeSurgery = new EyeSurgery();
$rightEyeSurgery->surgery_type_id = SurgeryType::query()->where('id', $data['right_eye_surgery_type_id'])->first()->id;
$rightEyeFields = new SurgeryField();
foreach ($data as $key => $value) {
if (str_starts_with($key, 'right_') && $key !== 'right_eye_surgery_type_id') {
$cleanKey = str_replace('right_', '', $key);
$rightEyeFields->$cleanKey = $value;
}
}
$rightEyeFields->save();
$rightEyeSurgery->surgery_fields_id = $rightEyeFields->id;
$rightEyeSurgery->save();

$data['left_eye_surgery_id'] = $leftEyeSurgery->id;
$data['right_eye_surgery_id'] = $rightEyeSurgery->id;

return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array
{
$leftEyeSurgery = new EyeSurgery();
$leftEyeSurgery->surgery_type_id = SurgeryType::query()->where('id', $data['left_eye_surgery_type_id'])->first()->id;
$leftEyeFields = new SurgeryField();
foreach ($data as $key => $value) {
if (str_starts_with($key, 'left_') && $key !== 'left_eye_surgery_type_id') {
$cleanKey = str_replace('left_', '', $key);
$leftEyeFields->$cleanKey = $value;
}
}
$leftEyeFields->save();
$leftEyeSurgery->surgery_fields_id = $leftEyeFields->id;
$leftEyeSurgery->save();

$rightEyeSurgery = new EyeSurgery();
$rightEyeSurgery->surgery_type_id = SurgeryType::query()->where('id', $data['right_eye_surgery_type_id'])->first()->id;
$rightEyeFields = new SurgeryField();
foreach ($data as $key => $value) {
if (str_starts_with($key, 'right_') && $key !== 'right_eye_surgery_type_id') {
$cleanKey = str_replace('right_', '', $key);
$rightEyeFields->$cleanKey = $value;
}
}
$rightEyeFields->save();
$rightEyeSurgery->surgery_fields_id = $rightEyeFields->id;
$rightEyeSurgery->save();

$data['left_eye_surgery_id'] = $leftEyeSurgery->id;
$data['right_eye_surgery_id'] = $rightEyeSurgery->id;

return $data;
}
and it works very well exept for file upload
drsdre
drsdre5d ago
The synthesizer error is coming from livewire/livewire/src/Mechanisms/HandleComponents/HandleComponents.php line 498. I would suggest to set a breakpoint on this line where $key=='' and check the context and path variables. I'm not familiar what the '->name("{$eye}_pdf")' part of the FileUpload definition is supposed to do. Maybe give it a try without.
Nashoba
NashobaOP5d ago
I tried to add it to see if it changes something after but it does nothing, I will remove it I'll check for the breakpoint, thank you ! I got this :
[2025-02-27 13:52:47] local.ERROR: Synthesizer error {"context":{"Livewire\\Mechanisms\\HandleComponents\\ComponentContext":{"effects":[],"memo":[],"component":{"mountedActions":[],"mountedActionsArguments":[],"mountedActionsData":[],"defaultAction":null,"defaultActionArguments":null,"componentFileAttachments":[],"mountedFormComponentActions":[],"mountedFormComponentActionsArguments":[],"mountedFormComponentActionsData":[],"mountedFormComponentActionsComponents":[],"mountedInfolistActions":[],"mountedInfolistActionsData":[],"mountedInfolistActionsComponent":null,"mountedInfolistActionsInfolist":null,"record":null,"data":{"operative_day_id":"1","doctor_id":11,"hour":null,"last_name":null,"first_name":null,"birth_date":null,"left_eye_surgery_type_id":"6","left_refraction_sphere":null,"left_refraction_cylinder":null,"left_refraction_axis":null,"left_white_to_white":null,"left_pupil_scotopic":null,"left_keratometry_3mm_k1":null,"left_keratometry_3mm_axis":null,"left_keratometry_3mm_k2":null,"left_keratometry_3mm_axis_2":null,"left_revision_needed":false,"left_comment":"","left_pdf":"","left_dominant_eye":"left","left_presbyopia_addition":null,"left_pupillary_offset":null,"left_treatment_type":null,"right_eye_surgery_type_id":1,"right_refraction_sphere":null,"right_refraction_cylinder":null,"right_refraction_axis":null,"right_white_to_white":null,"right_pupil_scotopic":null,"right_keratometry_3mm_k1":null,"right_keratometry_3mm_axis":null,"right_keratometry_3mm_k2":null,"right_keratometry_3mm_axis_2":null,"right_revision_needed":false,"right_comment":null,"right_pdf":[],"right_dominant_eye":"left","right_presbyopia_addition":null,"right_pupillary_offset":null,"right_treatment_type":null,"files":[]},"previousUrl":"http://localhost:8000/patients/create?operative_day_id=1"},"mounting":false}},"path":"left_pdf.c31da495-ebdb-4832-bf3d-89767c93ed27"}
[2025-02-27 13:52:47] local.ERROR: Synthesizer error {"context":{"Livewire\\Mechanisms\\HandleComponents\\ComponentContext":{"effects":[],"memo":[],"component":{"mountedActions":[],"mountedActionsArguments":[],"mountedActionsData":[],"defaultAction":null,"defaultActionArguments":null,"componentFileAttachments":[],"mountedFormComponentActions":[],"mountedFormComponentActionsArguments":[],"mountedFormComponentActionsData":[],"mountedFormComponentActionsComponents":[],"mountedInfolistActions":[],"mountedInfolistActionsData":[],"mountedInfolistActionsComponent":null,"mountedInfolistActionsInfolist":null,"record":null,"data":{"operative_day_id":"1","doctor_id":11,"hour":null,"last_name":null,"first_name":null,"birth_date":null,"left_eye_surgery_type_id":"6","left_refraction_sphere":null,"left_refraction_cylinder":null,"left_refraction_axis":null,"left_white_to_white":null,"left_pupil_scotopic":null,"left_keratometry_3mm_k1":null,"left_keratometry_3mm_axis":null,"left_keratometry_3mm_k2":null,"left_keratometry_3mm_axis_2":null,"left_revision_needed":false,"left_comment":"","left_pdf":"","left_dominant_eye":"left","left_presbyopia_addition":null,"left_pupillary_offset":null,"left_treatment_type":null,"right_eye_surgery_type_id":1,"right_refraction_sphere":null,"right_refraction_cylinder":null,"right_refraction_axis":null,"right_white_to_white":null,"right_pupil_scotopic":null,"right_keratometry_3mm_k1":null,"right_keratometry_3mm_axis":null,"right_keratometry_3mm_k2":null,"right_keratometry_3mm_axis_2":null,"right_revision_needed":false,"right_comment":null,"right_pdf":[],"right_dominant_eye":"left","right_presbyopia_addition":null,"right_pupillary_offset":null,"right_treatment_type":null,"files":[]},"previousUrl":"http://localhost:8000/patients/create?operative_day_id=1"},"mounting":false}},"path":"left_pdf.c31da495-ebdb-4832-bf3d-89767c93ed27"}
with this in the file : if ($key === '') { \Log::error('Synthesizer error', ['context' => $context, 'path' => $path]); throw new \Exception('No synthesizer found for key: "'.$key.'"'); }

Did you find this page helpful?