0

What's the best approach to store multiple images in database?

Upon browsing and searching for solution and approaches I found 2 options

first one is take the file name and concatenate and separate them using either comma or | but I'm having doubts since I think that's messy and would produce errors later in the system

second is I make another table for images and add foreign key (post_id) to know which post it belongs.

Any suggestion will be appreciated.

2
  • Just store your images as json. Perhaps under an images column, you can have {1: 'image1.png', 2: 'image2.png'} Commented Oct 23, 2022 at 19:48
  • 1
    Go with #2 - It's the correct relational design. And laravel can handle those "hasMany" and "belongsTo" relations quite well. Commented Oct 23, 2022 at 20:57

2 Answers 2

1

Both methods work well. I would place the second.

It can also provide convenience when using some image upload plugins (filepond, dropzone etc.).

If I were to choose the first method, I would save the serialized arrays (with serialize()) for better bet.

Sign up to request clarification or add additional context in comments.

Comments

1

Second one because it is normalized and you can have a better DX with that, No weird implementation to edit or any manipulation of post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.