Here’s an idea to use when database seeds need media assets.
Sometimes, for whatever reason, you want to seed your site with some assets, in addition to database seeds. Where do you put them?
I have a site where there are uploaded files in a folder public/uploads. In it are sub-directories for various types of media like images (in img) and so forth. My database seeds in database/seeds have references to these files, so I wanted the files to be in source control, so these files are available when cloning the project to a new location.
Normally you wouldn’t put uploads into version control, since that could bloat your repository with unnecessary files.But since there were only about 10 files, and it made the seed data refer to actual files, I figure they were worth committing.
To keep an uploads folder, so you don’t have to remember to create it when you deploy, you would typically use a .gitignore to ignore the contents of the uploads folder, except the .gitignore file itself. Git won’t commit an empty folder, so you have to put something in it to save it in version control. Having a .git ignore file there does the trick. Another convention is to simply add an empty .gitkeep file.
The .gitignore file would look something like this:
Seeds: Where Should I Put Them?
I thought about putting the files into the database folder, but decided on the resources folder, since they’re like assets. I put them into resources. I figured that would be a good generic location for them. I have the various sub-folders in there.
So now that the media is in resources, how do I get them where they can be linked to?
Elixir To The Rescue
I’m using Laravel Elixir to handle all the assets, so I added a copy command in my gulp.js file so the seeds copy over. That section looks like this:
mix .copy([ './resources/uploads/img/*.jpg', './resources/uploads/img/*.png' ], 'public/uploads/img') .copy('./resources/uploads/vids/*', 'public/uploads/vids');
I tried getting
'./resources/uploads/**.* to work, so I could copy the whole directory structure over to ‘public/uploads’, but I couldn’t get that working. I’ll tinker with it later to see what I missed. Your mileage may vary.
Just a tip that helped my process. Let me know if this is useful to you or other ideas you have for managing seed assets.