Korbe's avatar
Level 1

General Image Management Struggles - Eloquent - CMS

I built a website (Larvel/Vue) which lets you create a little information page. Some would say it's a CMS or a simple site builder.

Im currently trying to add images to the items* (see db below) but I'm struggling with the general workflow the laravel way. (no problems with basic upload/store though)

The information page is seperated into sections and for example a Team Section has TeamMembers (both are Eloq Models) and those TeamMembers should have an image(s).

My current db Schema looks something like this

users
	pages
		product sections
			products (items)
		team sections
			teamMembers (items)
		..

Images/Files

I think I'm overengineering at this point but as usual not quite sure.

My current prototype does not do much. Whenever a image/file is uploaded I move it in specific public folder and create one eloquent entry for that.

1 Question The image folder path is currently this /{county_code}/{page_id}/image_name. I'm wondering if this is a good idea though because I seperate the files. But what is best practice.

  1. 2 Or Should I put the all image just simply in one folder (or eg a folder for products/teammember) for all users?

2 Question Should I keep track of each image in the database (in an image morph Table)? Or just put the path('s) in the product model? If keeping track then why? (I would like to but..)

I once thought about an Wordpress-like Image Manager where you can manage all your images/files and when you are editing a product you can click "select image". Thus this includes you can use an image on multiple parts of the page what makes above approach with the morph mechanics more complicated (n:n moph).

2.1 I keep track of + it's dynamic and it's the laravel way, everything is connected. (?)

But again what advantage is given to me or user/customer. Just more work for me..

Thank you guys in advance!

0 likes
2 replies
alanholmes's avatar
Level 35

Hi @korbe

May I suggest using https://spatie.be/docs/laravel-medialibrary/v8/introduction for your image uploads.

It stores all the images in a single table, and uses polymorphic relationship to determine the model/record that the media belongs too (though this might not work if you are considering a WP style media gallery that can be used anywhere).

It handles a lot for you, and even gives you the option of generating different sizes (thumbnails, etc) and can also generate responsive images for you.

Using the above, to answer your questions:

  1. It places each image in a folder that contains the id of the media record, this way you never have to worry about name clashes. Tho the file paths can also be configured

  2. This takes the approach of a single table with a morph relationship

1 like
Korbe's avatar
Level 1

Hi, thanks for the reply.

I know this package but don't want to use it. I was asking for best practice tips, whether I should do or not do.

But I really appreciate your response.

I will stick with storing just the path names.

Thanks again!

Please or to participate in this conversation.