In this episode, we focus on how to add images to your products and product variations using the Laravel Media Library package. First, we talk about why images are important for both the main product and each variation, like showing a specific shoe color and size in a cart or on an orders page. We also set the stage for creating an image gallery, which will eventually be a Livewire component that lets users browse through multiple images for a product.
We start by installing the Laravel Media Library package, publishing its configuration and migration files, and running the necessary migrations. This sets up a media table in the database that uses polymorphic relationships, so you can easily link images to products or their variations. We also go over how to tune your storage locations and disk options, though we stick with the defaults for now.
Next, we make sure our models (like Product) are set up to handle media by using the right traits and interfaces. Then, we show you how to actually attach an image to a product via Tinker, and explain the importance of using features like preservingOriginal
to keep your source files safe.
A key aspect of this episode is setting up image conversions: automatically creating smaller thumbnails, so you don't serve massive images everywhere. We add a method to our Product model that creates a 200x200px thumb for any uploaded image. Finally, we test that everything is running smoothly by uploading a couple of product images (like black and white versions), and confirm that both the originals and their thumbnails are being saved properly.
By the end of this episode, you'll have the backend ready to upload and manage multiple images (and thumbnails) for products and their variations, setting you up for a sweet image gallery in the next step.