FileUpload (avatar) when record is in other model
I have made a
Product
model and a ProductAsset
model. Using mutateFormDataBeforeCreate() and afterCreate(), I create a new ProductAsset with "is_primary" flag to tell the app that the uploaded image is the product's "avatar". Then I can create more assets using a RelationManager.
So far so good. This is my form:
When viewing/editing that product, how I load that asset on the avatar field?
It's currently showing empty field. However, the image shows in the assets.

10 Replies
What is the issue exactly?
Since it’s working on your table it seems that the name in the make() on the form is wrong.
Maybe FileUpload::make(‘avatar’)
But if you have a custom saving strategy then you’re also going to need a custom hydration strategy.
There's no 'avatar' field on the database table really, is a related model ProductAsset
I've done a workaround
It's not the most elegant way tho
And it shouldn’t be necessary.
Is ‘image’ the relationship?
The relationship in the model is defined as:
But I need only the one with the field "is_primary" = true as a placeholder
Ok, that’s why it’s empty on the edit page. You are using ‘image’ as the field on the model, which doesn’t exist. So, use ->afterStateHydrated() to get the assets relationship and return the path for the first item on the relationship.
Basically, you have a custom write but not providing the inverse of a custom read. Hope that makes sense.
Ok I'll try and let you know the results 😉
Thanks!
I'll admit I don't know how to set the input to the already uploaded image.
So far I did this:
That's a better approach but I think I'm missing some method or concept to link the field state to the file without cheating the placeholder.
It's working tho

That's a very messy approach imho
I agree with you. Do you have any better approach?
Here is my (not so better) code right now btw
This way it shows the image, let the user change it, almost working perfect. But, It won't show the new image until I save changes on the form.