Fix media maxFiles(1) for Intertia requests #244
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently when using an image field that should only every have 1 file attached like this:
which would then be loaded and passed to inertia using the
loadResourceArray
method:an error will be thrown by laravel-inertia, as the response is not compatible.
This error happens because in
Ignite\Crud\Models\Concerns\HasFields
getMediaFieldValue()
it's defined to return the first media immidiately if maxFiles is set to 1:litstack/src/Crud/Models/Concerns/HasFields.php
Lines 133 to 141 in 5814831
This however seems to be incompatible with the this line form
Ignite\Crud\FormResource
toArray()
method:litstack/src/Crud/FormResource.php
Line 73 in 5814831
My suggestion here is a simple addition to check if the field is an instance of
MediaField
and directly use the method used inHasFields@getMediaFieldValue
without the reassignment if it's only one file.I think this is appropriate fix as I'm not sure what would break if I changed the
getMediaFieldValue
method directly (although the backend doesn't actually seem to care).