Skip to content

Conversation

lpheller
Copy link
Collaborator

Currently when using an image field that should only every have 1 file attached like this:

$form->image('image')
    ->maxFiles(1);

which would then be loaded and passed to inertia using the loadResourceArray method:

return Inertia::render('Home/Home', [
    'form' => HomeConfig::loadResourceArray(),
]);

an error will be thrown by laravel-inertia, as the response is not compatible.

Error
Call to undefined method Symfony\Component\HttpFoundation\StreamedResponse::getData()

Inertia\Response::Inertia\{closure}
vendor/inertiajs/inertia-laravel/src/Response.php:87

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:

public function getMediaFieldValue(MediaField $field)
{
$media = $this->getMedia($field->id);
if ($field->maxFiles == 1) {
$media = $media->first();
}
return $media;
}

This however seems to be incompatible with the this line form Ignite\Crud\FormResource toArray() method:

$value = $this->getAttribute($field->local_key);

My suggestion here is a simple addition to check if the field is an instance of MediaField and directly use the method used in HasFields@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).

@lpheller lpheller requested a review from jannescb August 26, 2021 10:21
Copy link
Collaborator

@jannescb jannescb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@cbl cbl merged commit f6187c6 into 3.x Sep 8, 2021
@cbl cbl deleted the fix-max-media-files branch September 8, 2021 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants