Storing Images on the Filesystem

As handy as it can be to store images in the database, sometimes you just want to store them in the Filesystem. Luckily this is relatively straightforward also. Imagine, for example, we just want to add an image for each Member of the tennis club and view it on the members index page. We just need an additional column in the Member table to store the file name. Then when the user uploads a picture of themselves we need to generate a unique filename, store the file in the file system with its new name (and the correct file extension of course e.g. jpg/png/gif etc).

Then when we want to render the file we can use the laravel blade asset directive to access the file (so long as it's stored in some subfolder of the public folder).

To add an additional column to the member table, execute the following SQL command on your database.

ALTER TABLE member ADD COLUMN imgfilename VARCHAR(50);

To allow the user to upload an image file we must change the FORM tag to allow it to encode multipart data and we have to add an extra field where we can attach the file. The Laravel view we will modify is \resources\views\members\create.blade.php. Find the Form tag