0

I have a Laravel model which is called for example, Product, with MySQL table products underneath. In my React SPA, user have the ability to set background of the product which can be a color (assigned in my React SPA through css), or an image uploaded by that user. The product can be either private, or public (accessible by anyone)

What's the best way to accomplish this?

Solution 1: Store image BLOB or dataURL in MySQL field. Similar to what is described here

Solution 2: Use Laravel Storage - So on altering the product, I would put the image in the Storage, and only store link to that image in MySQL field. Then in my React app, after fetching product, I would need to request the image again (CSS would do that using link provided from the response)

I'm curious if solution 1 is worse because of performance. But as I'm looking at the solution 2, I realize that putting the file in the Storage would require that file to be public. Which solution would you pick and why?

papryk
  • 442
  • 4
  • 14
  • 2
    I would store the images with the Storage facade and save the name into the DB. I would also add a field to the DB for the public/private option. Afterwards you can either save the image to the private storage or public storage. – Aless55 Nov 04 '21 at 10:40
  • 1
    You have only one solution - two. First is not a solution - it's a problem. – Maksim Nov 04 '21 at 10:44
  • Thanks @Maksim, could you explain further why is that a problem? MySQL, Postgress support binary data fields. I'm just curious. – papryk Nov 04 '21 at 10:48
  • Read here, for example: https://www.quora.com/Why-is-it-considered-bad-to-store-images-in-a-database – Maksim Nov 04 '21 at 11:04
  • Does this answer your question? [Storing Images in DB - Yea or Nay?](https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay) – Don't Panic Nov 04 '21 at 11:25
  • Thanks, it all does. Basically I understand now why I should use file storage instead of storing binary data in database. I wanted to store it in db as it seem to be a quicker solution for me, but it can be a problem at scale. Thanks a lot – papryk Nov 05 '21 at 08:59

0 Answers0