hippo's avatar
Level 3

Issues with Serialization of Uploaded File

Hey guys, I'm confused- I'm trying to use a Laravel\Nova\Actions\Action to upload a file.. Can someone help me understand the best practice for this? As is, I get error Serialization of UploadedFile is not allowed.

Thanks in advance!

class ProvideMoreInformationFor extends Action implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

   /**
     * 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 $job) {
            $file = Storage::get($fields->file);
            Storage::move($fields->file, "/app/jobs/" . $job->folder . "/" . $file->getClientOriginalName());
            Action::message('Added file to Job!');
        }
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return [
            File::make('File')->rules('required')
        ];
    }
}
0 likes
1 reply
hippo's avatar
Level 3

The issue here was in the nova documentation: The issue was mainly in using queuable with file uploading. I also changed the storage code to function better. Hope this helps someone!

class ProvideMoreInformationFor extends Action
{
    use SerializesModels;

   /**
     * 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 $job) {
            $fields->file->storeAs('jobs/'.$job->folder, $fields->file->getClientOriginalName(), 'local');
        }
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return [
            File::make('File')->disk('local')->rules('required')
        ];
    }
}
2 likes

Please or to participate in this conversation.