In this episode, we tackle the problem of products that don't have images—something that's bound to happen at some point! You'll see how we can provide a fallback image to display whenever a product is missing pictures, so our site never looks broken or empty.
First, we create a new product in the database without attaching any images, just to show what happens. When we browse to this new product, there's a blank space where the image should be—definitely not ideal for a user.
The fix: we'll add a fallback image to the project. You'll watch as we drop a generic image (something like a "no image available" placeholder) into the public directory, and, with a small change to the Product model, wire it up so that any product missing images will automatically use that fallback. We'll use a method from the media library for this, specifying the fallback URL. (Just remember, you need your own placeholder image for this part!)
We run into the classic issue of making sure images stored in the app are publicly accessible. So if the fallback doesn't work right away, don't worry—we show how to run php artisan storage:link
to make sure Laravel serves up the images correctly.
That's it! Now any product without a specific image just shows the fallback. In the next episode, we'll take this further and apply the same approach to product variations.