This episode is for members only

Sign up to access "Build an E-Commerce Platform" right now.

Get started
Already a member? Sign in to continue
Playing
14. Creating the product gallery

Episodes

0%
Your progress
  • Total: 8h 42m
  • Played: 0m
  • Remaining: 8h 42m
Join or sign in to track your progress
01. Introduction and demo
9m 42s
0%
02. Installing Laravel, Breeze and Livewire
4m 47s
0%
03. Creating categories
9m 4s
0%
04. Recursively displaying categories
8m 21s
0%
05. Product model and migration
2m 50s
0%
06. Showing a product
7m 50s
0%
07. Product variation setup
10m 26s
0%
08. Creating the product selector
10m 34s
0%
09. Loading child variation dropdowns
4m 28s
0%
10. Faking adding the final variation
9m 33s
0%
11. Setting up product stock
4m 34s
0%
12. Calculating variation stock levels
9m 1s
0%
13. Adding product images with MediaLibrary
8m 50s
0%
14. Creating the product gallery
7m 28s
0%
15. Providing a fallback image
2m 56s
0%
16. Adding media to product variations
3m 37s
0%
17. Creating the cart model
3m 37s
0%
18. Registering the cart service
6m 34s
0%
19. Creating a cart session
11m 4s
0%
20. Showing the cart in the navigation
9m 19s
0%
21. Caching the cart instance
3m 2s
0%
22. Adding items to the cart
14m 1s
0%
23. Creating the notification component
8m 5s
0%
24. Showing the user's cart
6m 50s
0%
25. Outputting cart items
4m 50s
0%
26. Showing variation specifics
8m 8s
0%
27. Updating item quantity
8m 2s
0%
28. Removing an item from the cart
6m 1s
0%
29. Calculating the cart summary
8m 7s
0%
30. Showing the category products page
5m 1s
0%
31. Indexing products in Meilisearch
8m 32s
0%
32. Hooking up products to categories
4m 15s
0%
33. Building the product browser
13m 32s
0%
34. Showing child categories
1m 51s
0%
35. Indexing product variations for filtering
8m
0%
36. Outputting variations for filtering
12m 52s
0%
37. Hooking up product filters with Livewire
7m 48s
0%
38. Filtering products
12m 24s
0%
39. Filtering by price
9m 50s
0%
40. Adding global navigation search
7m 50s
0%
41. Handling products that are not live
3m 22s
0%
42. Price range category fix
1m 18s
0%
43. Scaffolding the checkout page
8m 6s
0%
44. Listing shipping options
9m 3s
0%
45. Calculating the cart totals
2m 52s
0%
46. Validating the account form
9m 10s
0%
47. Validating the shipping form
5m 47s
0%
48. Saving the shipping address
8m 4s
0%
49. Selecting a saved shipping address
6m 39s
0%
50. Fix shipping address error for non authenticated users
1m 7s
0%
51. Redirecting if the cart is empty
2m 42s
0%
52. Checking for quantity changes
7m 11s
0%
53. Syncing if quantities have changed
11m 59s
0%
54. Flashing a message when quantities have changed
5m 48s
0%
55. Setting up for orders
5m 17s
0%
56. Creating an order
13m 24s
0%
57. Attaching variations to order
6m 13s
0%
58. Reducing stock after ordering
2m 56s
0%
59. Meilisearch filter query fix
1m 33s
0%
60. Showing the order confirmation page
7m 39s
0%
61. Attaching orders for registering guest users
5m 17s
0%
62. Scaffolding the orders page
5m 30s
0%
63. Filling in order variation details
5m 24s
0%
64. Returning the order status
4m 39s
0%
65. Detecting order status changes
10m 49s
0%
66. Sending the order status change email
5m 12s
0%
67. Sending an order confirmation email
2m 47s
0%
68. Handling deleted cart records
4m 44s
0%
69. Transferring the guest cart
2m 44s
0%
70. Creating a presenter for the order status
4m 31s
0%
71. Setting up Stripe
3m 43s
0%
72. Creating and updating a PaymentIntent
16m 21s
0%
73. The Stripe card form
3m 35s
0%
74. Validating before payment
5m 34s
0%
75. Submitting a payment
6m 40s
0%
76. Checking for a successful payment
5m 47s
0%
77. Handling Stripe client errors
3m 11s
0%
78. Entangling Stripe customer data
2m 18s
0%

Transcript

00:00
So we're ready to build out our image gallery. Let's start with the Livewire component.
00:04
So let's go ahead and say php artisan make or Livewire make. And we're going to call this product gallery. And let's create that out. Great.
00:15
So if we head over to the product gallery blade template, once again, I'm just going to paste some pre-created markup in here. You can either copy this or grab it from the course resources. Essentially, this just is a outer wrapper with the main product image at the top here.
00:32
And then we've got a grid here with six columns. So six individual thumbnails will be output below with a gap of two. And they'll be clickable. And when we click them, that will swap the thumbnail for the main image.
00:46
Sounds a bit more complicated than it actually is. So if we go over to our product show page, let's pop the product gallery in here. And let's kind of think about the kind of information that we'd want to pass through to the product gallery.
01:02
We need to extract the images out. So I'm actually going to pass the entire product into here. That kind of makes sense. And then that's pretty much it.
01:10
We just want to grab the first piece of media from this product, display it inside of the big one here. And then we want to iterate through and show all of the small ones. So if we come over to our product gallery live wire component,
01:26
when we mount this component, we want to grab that first image. Now, let's just go ahead. And before we grab that first image, grab the product just up here. And just dump on all of the media that this contains, just so we can see what we get back.
01:43
So if we just go ahead and say this product. And if we just dive into the source for that has media or interacts with media, let's take a look here at the media relationship here. So we've got this morph many, like we said, it's a polymorphic relationship.
02:01
So let's go ahead and just dump that out. So let's go back over to our product gallery and just say media just to see what we get. So let's give that a refresh. And inside of here, we have two media classes with all of the information that we need.
02:15
Now, each of these media classes, which are technically models, will contain the methods that we need to extract that information out. So we can grab, for example, the URL for this particular piece of media, whatever we need. So it's a simple relationship with their methods on their models.
02:33
But we also have overall methods, the way we can grab the URL for the first media, just to make things a little bit more convenient. Otherwise, we would have to do media first and then use some sort of method to get the URL, which we don't really want to do.
02:46
So we're going to go ahead and store the selected media URL up here. And this is key because we need to swap this. So let's say selected image URL. And by default, that's just null.
02:57
And let's go ahead and set this in here when we boot this up. So we're just going to say this and product. And we're going to say get first media URL. I think it is.
03:09
Let's just test this out by going over to our product gallery template and just dumping this on the page. So let's just reference that selected media URL in here and see what we get. So let's come over, give this a refresh.
03:21
And there we go. So we've got the full URL to the project, which we might need to change. And then storage slash two slash Air Force One black. So let's go ahead and just make sure that this URL works.
03:32
Just make sure we don't need to add any sim links or test anything. And there we go. Beautiful. There is our shoe.
03:38
So we can output that in that image tag by just binding it in. So let's go ahead and just grab the entire thing. Pop that in there. Come over.
03:46
And there we go. There is our product image. And of course, you can add styles to this if you need to resize it or anything like that. OK.
03:52
So let's focus on our thumbnails here. So we want to iterate over these anchors and output the thumbnails for each of our items. How are we going to do that? Well, if we go back to our template, we don't actually need to do anything for this inside
04:07
of the Livewire component. Let's bring this in a little bit. All we need to do is iterate over all of the media. So we're just going to go ahead and say for each product, get media as media.
04:22
And let's just test this out before we do anything. So before we start to pop this into an image tag, let's see what we can do. So let's say media get URL. And I think that should be it.
04:34
Let's just see what we get. OK. There we go. We've got the two items in here.
04:38
So we've got the white and the black version, which we can now technically put into that image tag. However, notice that this is a link to the large version. Now, that would work, but it would be a waste putting a really large image into such a small
04:56
thumbnail. So what we can actually pass into this is the name of the conversion that we previously defined over in our model, which is thumb 200 by 200. So let's go and just put that into this as a parameter and see what we get back.
05:12
And you can see here now that it's referencing that conversions folder. And this will be a much smaller version of the image. Now, this is a bit hard to select at the moment because we've got this anchor in the way. Let's just get rid of that for now.
05:25
And let's pull this into a new tab. And there we go. There is our 200 by 200 version. We can even make that a little bit smaller.
05:35
Or you could define another conversion if you wanted to. So there's nothing to stop you going ahead and doing this and doing a 100 by 100 version if you needed it. We're just going to keep one for now, but feel free to play around.
05:49
OK. Now that we've done that, we can go ahead and paste this inside of here. And we can grab the URL and put that in the image tag. Let's go over.
06:00
And there we go. There are two small thumbs in here that we can now click on. Now, technically, these should be buttons. So let's just switch that over to a button type because we don't really go anywhere with
06:11
these when we click on them. We just want them to perform some sort of action. And the action that we want them to perform is to swap this with this large image. So how are we going to do that?
06:20
Well, in this case, we can attach a wire click. And we don't need a prevent because we've switched that to a button. And we'll just call some sort of select image method over on our live wire component. And into this, we'll go ahead and pass in the media URL.
06:36
Now, what we don't want to do is pass the thumbnail in because if we were to do that, then the thumbnail would then become the large image, which would look a bit small. So we're going to get rid of the thumbnail reference in there. So we're showing the thumbnail.
06:49
But when we select the main large image that we want to show, we just use get URL. So let's implement this select image method inside of here. So let's go ahead and create this out. And of course, that's going to take in the URL that we want to show.
07:05
And we can just basically replace that with selected image URL. So we're just going to say this selected image URL and URL. Let's try it out. Let's go over, click on white.
07:16
And there we go. We can now toggle between them two images. Very, very simple. But with live wire, very quick to do and very effective for a very simple product page.
78 episodes8 hrs 42 mins

Overview

Build a robust e-commerce platform with a Laravel and Livewire. Features products with unlimited and flexible variations, a product browser with filters and price range slider, global product search, guest checkout, shipping and payment implementation, order status tracking and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.