In this episode, we finally tackle the process of storing a user's profile photo in our Laravel application. Until now, we could select a photo, but it wasn't actually being uploaded or saved anywhere. So, here’s what we do step-by-step:
First up, we make sure our form knows about the new photo—we add the necessary data property and use a Vue watcher to tie the selected file to the form. We do a quick test to check the file is actually part of the data we’re sending off.
Next, on the backend, we update our process to accept profile photos. We realize that we need to change how our frontend sends form data to the server (switch from PUT to POST but spoof the method, so Laravel's Fortify still handles it correctly).
On the database side, we create a migration to add a new profile_photo_path
column to the users table, so we have somewhere to store the uploaded file's location.
To keep things tidy, we separate out the profile photo updating logic into its own action and interface. This makes it reusable and easy to manage on its own. We include the new action wherever we need, bind it properly in the service provider, and make sure when a user uploads a new photo, it stores the file on disk and updates the database with that path.
We cover file validation (only images, max 5MB), and finally, ensure the user's avatar URL gives us the public URL to the image, so it shows up right away.
To finish, we do a recap: we’ve added a database column, changed the way the form submits, separated our photo-upload logic, and made everything work together so users can now upload and update their profile pictures. All done!