#Passing a DTO to job, UploadedFile not serializable

15 messages · Page 1 of 1 (latest)

novel matrix
#

I am using the laravel-data from Spatie package. I have a DTO, and I want to pass it to a job. The DTO has 3 instances of 'UploadedFile', but when passing it, it errors Serialization of 'Illuminate\Http\UploadedFile' is not allowed. Even when excluding the files from the DTO, it still gives this error. How can I fix this, or is my approach wrong?

calm stone
#

You’ll need to save the files first.

novel matrix
#

Thanks @calm stone. The thing is that the job takes a RegistrationDto, so we can, inside of the job, easily use the properties of this DTO.

Should we avoid passing the DTO directly to the job? I've tried to convert the files to base64, which works fine (inside of the controller). But, when then passing the DTO / excluding these file fields from it, it still gives the error.

calm stone
novel matrix
#

I see.

So should we get rid of the Dto class as the job parameter? And manually add every property of the Dto to separate parameters?

#

So, In the controller:

Convert all files to base 64

Pass the base 64s to the job

Pass the other data from the Dto to the job, but not as a Dto instance

calm stone
#

Store the files to disk. Then pass the paths to the job. The job can retrieve the files from disk when the worker picks it up.

#

Similarly to how a full Eloquent model isn’t stored in a job; just it’s ID. The job then retrieves the full model from the database when it’s proceeded by a queue worker.

novel matrix
#

Alright, makes sense.

And for the DTO to the job? Since the object still contains 3 UploadedFile instances now, so it will error (even though I will handle the file upload inside of the job by passing the file path). The DTO is validating the request from the API as well.

calm stone
#

You need to handle the file uploads before the job is dispatched; not during.

novel matrix
#

Yes, but the DTO contains much more information that we need to use inside of the job. The files should be removed from that DTO ideally, but that doesn't work for some reason (using ->except).

#

We will handle the files before the job is dispatched.