Sam's avatar
Level 4

File uploads - saving to different models

Hi folks,

Hope you can help, this is my situation which has arisen from migrating an old precedural business application to Laravel. Within the old precedural system we have a number of documents that can be uploaded, unfortunately, for each type of document we have separate tables, for example we have, quotedocuments, supplierdocuments, clientdocuments along with 4 others.

Now I know the simplest solution would be to simply have one table that handles the uploaded files but unfortunately due to legacy data and other business applications that require continuation we're unfortunately unable to do anything about this particular approach.

With this in mind I was wondering how you might approach this situation, the criteria being:

  • Multiple files can be uploaded at once via dropzone.js
  • The files should be saved to its own respective directory within the 'public' storage type
  • The information relevant to each file "type" should be saved to its respective table.

The way I have it at the moment is as follows:

  • Dropzone.js posts to the route which calls the relevant controller method
  • This controller method setups up validation and then hands off to a service class
  • The service class then calls off to a factory which calls a method "getFileHandler" which simply does a switch statement based on a 'fileType' parameter passed through with the request data from dropzone.js
  • This factory then returns the relevant model with a custom method of getData() which simply sets up an insert array.

I can't help feeling this seems to be such a complex way of doing things. Bearing in mind that the implementation could be used in several different places across the application.

Any pointers/guidance/tips would be greatly appreciated.

Cheers

0 likes
1 reply
cecosd's avatar

@sam

Hey Sam, Personally I would do the same as you did.

Dropzone N files with different extentions.

In the controller method I would get the type of document (extention)

For every extention I would make a model, ex. JpgFile, PngFile and I will create a new record on upload.

Every model will have some own rules for creating new records.

If you like you can share some code and we can talk about it

Please or to participate in this conversation.