advanced-nova-media-library icon indicating copy to clipboard operation
advanced-nova-media-library copied to clipboard

Action multiple upload

Open keizah7 opened this issue 4 years ago • 6 comments

Package not work in action image Can't add multiple files and parse it from request

keizah7 avatar Oct 11 '21 13:10 keizah7

I have been able to use in an action with a little workaround.

/**
 * Perform the action on the given models.
 *
 * @param  \Laravel\Nova\Fields\ActionFields  $fields
 * @param  \Illuminate\Support\Collection  $models
 * @return mixed
 */
public function handle(ActionFields $fields, Collection $models)
{
    foreach ($models as $model) {
        /**
         * @var $media \Ebess\AdvancedNovaMediaLibrary\Fields\Media
         * @var $model \Spatie\MediaLibrary\HasMedia
         */
        $attribute = 'material';
        if($mediaClosure = $fields->callbacks->get($attribute)) {
            $media = new \ReflectionFunction($mediaClosure);
            $files = $media->getStaticVariables()['data']  ?? new \Illuminate\Support\Collection();
            foreach ($files as $file) {
                $model->addMedia($file)->toMediaCollection($attribute);
            }
        }
    }
}

/**
 * Get the fields available on the action.
 *
 * @return array
 */
public function fields()
{
    return [
        Files::make('Material', 'material'),
    ];
}
`

leoflapper avatar Nov 18 '21 09:11 leoflapper

@leoflapper Are you able to upload multiple files?

With Laravel 8 + Nova 3 I am not able to add multiple files. Anytime I press Upload New File to select an additional file it replaces the previously selected file, instead of appending it to the list.

When I use the field on a Nova resource, the text on the button is Add New File, but when I use it with an Action, the text is Upload New File

I also tried it with Images, but it also has the same behavior, always replacing the previously selected image, instead of appending to the list.

kukukk avatar Feb 17 '22 13:02 kukukk

It seems that when used in Action, it is not able to identify the collection and get the information whether it is as singleFile() or not.

kukukk avatar Feb 17 '22 14:02 kukukk

@kukukk I just tested it, but I am also not able to upload multiple files with the same upload field. I can only retrieve a single file.

This works. But it is a not a very user friendly solution. Especially with a lot of files.

protected int $maxFiles = 5;

/**
 * Perform the action on the given models.
 *
 * @param  \Laravel\Nova\Fields\ActionFields  $fields
 * @param  \Illuminate\Support\Collection  $models
 * @return mixed
 */
public function handle(ActionFields $fields, Collection $models)
{
    foreach ($models as $model) {
        /**
         * @var $media Media
         * @var $model HasMedia
         */
        for ($x = 1; $x <= $this->maxFiles; $x++) {
            if($fileClosure = $fields->callbacks->get('file_'.$x)) {
                $this->uploadFiles($fileClosure, $model, 'file');
            }
        }
    }
}

/**
 * @param \Closure $mediaClosure
 * @param HasMedia $model
 * @param string $collection
 * @throws \ReflectionException
 * @throws \Spatie\MediaLibrary\MediaCollections\Exceptions\FileDoesNotExist
 * @throws \Spatie\MediaLibrary\MediaCollections\Exceptions\FileIsTooBig
 */
private function uploadFiles(\Closure $mediaClosure, HasMedia $model, string $collection)
{
    $media = new \ReflectionFunction($mediaClosure);
    $files = $media->getStaticVariables()['data'] ?? new \Illuminate\Support\Collection();

    foreach ($files as $file) {
        $model->addMedia($file)->toMediaCollection($collection);
    }
}

/**
 * Get the fields available on the action.
 *
 * @return array
 */
public function fields()
{
    $fields = [];
    for ($x = 1; $x <= $this->maxFiles; $x++) {
        $fields[] = Files::make('File '.$x, 'file_'.$x);
    }
    return $fields;
}

leoflapper avatar Feb 17 '22 14:02 leoflapper

@leoflapper Thanks for the workaround idea. In case my clients wants the possibility to upload multiple files in one step this seems to be the only solution now.

Are we missing something? Or any plan to make Advanced Nova Media Library fully supported in actions?

kukukk avatar Feb 18 '22 11:02 kukukk

@kukukk @leoflapper

  • Solved by using with meta to add multiple attribute

Files::make('Documnets', 'documents') ->rules('required') ->withMeta(['multiple' => true])

AbdallahAli912 avatar Jan 19 '24 15:01 AbdallahAli912