Image (Pro) field with Polymorphic relation #704
-
I'm having an issue with the Image (pro) field when using it to upload image to a model related via a polymorphic relation. I've created an example site with minimum code to reproduce the issue: The site has three models:
Book and Author have a polymorphic relation to the Attachment model. When using the Image for 'Author' with name Is there any specific configuration to get this working? Or is there something I'm doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @vdeep! In order to achieve this, you should use a "MorphOne (1-1 polymorphic) + subfields" field. Basically, you should setup your Backpack will handle the rest for you 👌 CRUD::addField([
'name' => 'attachment',
'label' => 'Attachment',
'subfields' => [
[
'name' => 'filename',
'type' => 'upload',
'withFiles' => [
'disk' => 'public',
'path' => 'uploads',
],
],
// more fields may go here...
],
]); You may add other fields to your relationship if needed, like a displayable attachment name. Thank you by the provided sample code, it was helpful to quickly setup and fix the issue 👌 |
Beta Was this translation helpful? Give feedback.
Hi @vdeep!
In order to achieve this, you should use a "MorphOne (1-1 polymorphic) + subfields" field.
You have an example in our demo 👌
Basically, you should setup your
attachment
relationship field with subfields, and one of those subfields (in your case the only one) should be yourfilename
which is anupload
field type.Backpack will handle the rest for you 👌