hakhsin's avatar

Which one is better?

Hi, imagine there are a product entity. Every product has at least 1 image and a maximum of 5 images. If I name every product in this format product-id_image-number, which one of bottom practice is better?

1- I assume every product has 5 images and in frontend side I request for get them. I get this object from server:

{
    "title": "Test Title",
    "description": "This is a test",
    "id": 106
}

And now I request www.exm-url.com/product-106-1 and etc. If the image exists I get it else I get 404.

2- The server give all image urls. Like this:

{
    "title": "Test Title",
    "description": "This is a test",
    "id": 106,
   "media":[
       "www.exm-url.com/file_name-12123134234",
       "www.exm-url.com/file_name-24334534646",
   ]
}

Which performance of theirs is better? Do I make a query on database for get images or I define a format of file name? May be there are 1000000 records in media table after one years.

0 likes
2 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

A query to get only images you need is more efficient.

2 likes
Snapey's avatar

Use Spatie media library. It will solve all these problems for you.

1 like

Please or to participate in this conversation.