Action multiple upload
Package not work in action
Can't add multiple files and parse it from request
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 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.
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 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 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 @leoflapper
- Solved by using with meta to add multiple attribute
Files::make('Documnets', 'documents') ->rules('required') ->withMeta(['multiple' => true])