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
04. Recursively displaying categories

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 have this category tree nicely structured for us, how are we going to start to iterate over this? Well let's start with just a really simple for each loop over in home.blade.php. So we'll redesign this in a second but let's go ahead and say for each categories as category and in that for each and let's go ahead and just output the category title. So category
00:26
and title. Let's go over give that a refresh. So we see brands and we see seasons. Now let's just wrap this in a div just so we get a little bit of spacing out here just to play around with this and then we'll go ahead and create a component which will recursively call itself to iterate through and give each of the items underneath this. So before we do that let's just take a
00:48
look at an example. So we know that we've got a category children property so what we could what we could technically do is copy this block here and then go underneath here and say for each category and children let's just fix that up as child for example and this is not a sustainable way of doing things unless you know that you have a strict number of nests. Then we could say child
01:15
title as well and we could even go ahead and just add a little bit of styling to here. If you're unaware Breeze uses tailwind so we're going to go ahead and say margin left of four. There we go. So we're starting to build this tree up. So this works well but if for example under Nike we wanted to add another category so if we duplicate this down and say new in and just call this Nike new
01:39
in the parent id for this would be three. Now if we save this out we're not going to see that so the more categories that we add we're going to have to add more custom for each loops. Now like I said that's not sustainable because you want to probably just add categories when you want and not have to come back and update your code. So to get around this what we're going to do is
02:01
create a blade component which deals with outputting the category and the category children but this is going to recursively call itself. So let's go over and create this component. So let's say php artisan make component and we'll just call this category. Okay so now that we've created that component I'm going to go up to the view folder here and I'm going to delete the base
02:26
class for this because we're not really going to need this. We'll just need the blade component itself and what you might need to do is run php artisan view clear just to clear that out. Okay so now that we've done this what we can do is grab the category title get rid of that and instead of this in fact we could grab the whole lot here we could say x category and we can pass the category
02:50
through as a kind of prop. Now inside of this category component which will be in views and components just over here we can paste this in. Now that's going to work in exactly the same way as we would expect. So let's go over and just give that a refresh and you can see it works in the way that we originally saw. But now that we've got a component in here what we can do
03:14
is add that for each loop to the category children but call this same component to render itself again and that will recursively go through and always output all of the category children. So let's try this out we're going to go and say for each category children as child we're going to end that for each loop there and then inside of here we're going to call the same component
03:41
and pass down as the category prop the child itself. So the next loop of this is going to pass the child down as the category and if this child has any children then it will then go ahead and for each loop again. So like I said we have a snippet on this over on codecourse so you can go ahead and check this out if you need a bit more information. Now let's again just go ahead
04:05
and add in a margin left to this just so it kind of spaces things out and you can see here that we are now recursively iterating through each of the categories. So if I were to add another category under new in that would appear so we can just add as many nested categories as we like and again we're only running one query for this so it's nice and fast. Okay let's go ahead and style this up
04:30
just a little bit if you want to. So over in the main page here I'm actually going to go ahead and get rid of this header slot that might break things and yeah it does. So if we just come over to app.blade.php really quickly we could probably add a really simple is set check in here. So let's grab this header variable and let's say is set header and let's go ahead and end that is set
04:52
there indent that and there we go we don't need that header in there anymore. All right so over on the home page we're going to go ahead and get rid of this background here so we could probably just move that out get rid of the shadow because we're not going to need that either and we could probably bring this overflow hidden down to here just to get rid of one level of nesting like so.
05:19
Let's go over and have a look yeah it still looks like we've got a back yeah we have background and we can get rid of this border as well let's give that a refresh. Okay so that's looking a little bit better. Okay so let's go over and just change this around a little bit so each of the top level categories are next to each other to do this we're just going to use grid and let's just say we want
05:39
this to be a no column grid on small and then when we get up to medium we want this to have two columns and when we get up to large we want this to have four columns and let's give that a refresh and let's also make sure we add the correct class name in there we go so we've got two next to each other great okay so we're going to go and change the gap here to four and let's
06:07
give that a refresh that should be everything that we need so we've got two of these categories next to each other if we just double check this with our developer tools and we pull this in slightly you can see that they just drop down next to each other as we get a little bit smaller you can always adjust this we just want something that looks a little bit better okay so for each of the categories
06:28
i want the very top level to be a different class so if we come over to our category component here we're going to add a little check to see if the depth is zero eg the first item so i'm going to go and just wrap the title in an anchor because we're going to need that to click through to somewhere at some point and let's go ahead and add in a class here we're going to say in here
06:54
category and depth and check that at zero if that's the case we're going to set the font to bold otherwise we're just going to do nothing so if the depth is zero e.g the top one just here we want that to be a font bold now at the moment we don't see this as bold we just need to go ahead and rerun npm run dev just to compile everything the reason that this isn't working is because
07:19
under our tailwind configuration these are the three places that we look for our tailwind classes to then build our final bundle to get this to work again we're just going to have to run npm run dev and just wait for that to finish so now that's done if we come over there we go sure enough we've got brands and seasons and also some of the grid stuff has now worked a little bit better now
07:40
that we've pulled that in okay so i think the last thing we'll do is just get rid of this padding because that's just going to pull that in a little bit and there we go we have our categories now of course at the moment these don't link through to anywhere but each of these categories will have a url so we could go ahead and fill that in now because we pretty much know what
07:57
that's going to be categories slash and then category slug so let's just check this out and if i click on brands sure enough we go through to the overall brands category like so there are categories you can make them look as pretty as you like but for now that will do we have an unlimited nest of categories that we can add with a single query
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!

Episode discussion

No comments, yet. Be the first!