Planning Increment Item0:00
So let's see if we can complete the add1 function by actually adding a card item to the card. From the card controller here, in an ideal world, as you can see Capilot trying to hint, we would just call something like add product with the quantity of one. But here I don't even want to have to care about the quantity and we are going to use the same function for adding a product or incrementing a product for a specific card item. So I think I will call this wishful thinking method increment item.
item. So I think I will call this wishful thinking method increment item. That's not going to work because the card does not have an increment item function just yet. But I think naming wise, it makes sense. If we have zero items, we increment and it gives us one. If we have two items, we increment and it gives us three. So what we want to do now is go in the card model and implement this increment item that should take care of either creating a new card
card model and implement this increment item that should take care of either creating a new card item entry or incrementing the existing card item quantity by one. The cool thing here is in our controller, we don't have to worry about what current quantity it is. We just call increment item because the function is called add one. And then the cart model is going to have the logic for that. And before I leave this so that we don't end up on the new route, let 's return back
Implementing Cart Increment1:19
for that. And before I leave this so that we don't end up on the new route, let 's return back so that we reload the welcome page when this has run. Okay, so in the cart model, after our if exists and ensure exists function, let's implement a public function increment item. It's going to need to receive a product. And here we're going to do something very similar that we've done for the ensure exists, we're going to use first or create, but not for the
that we've done for the ensure exists, we're going to use first or create, but not for the cart, but for the cart item. So instead of static, we are going to use these items. Remember, this comes from the items function here, which is the relationship with the cart item. And here we're going to use first or create, which receives two arguments. The first one is the property we want to match in our case, the product ID. So if the items product ID matches the product ID that
want to match in our case, the product ID. So if the items product ID matches the product ID that is passed to that function, it means we have found the first instance, otherwise we create it. And then we need to determine what we want to insert if it did not exist. So if it hasn't found this first item, it's going to insert. And here we want a quantity of zero. Basically, we instantiate a new cart item with the quantity of zero. And the reason is immediately after
we instantiate a new cart item with the quantity of zero. And the reason is immediately after creating it or finding it, we are going to use the increment function available here. And we're going to increment the quantity field. Hopefully that makes sense. We receive a product, we search in the cart items, if we find this product, if not, we create it with a quantity of zero, and then we increment. So if there was a cart item with three quantity, it would increment it
and then we increment. So if there was a cart item with three quantity, it would increment it and make it four. If the product did not exist, it starts with zero, gets incre mented by one, and therefore has the value of one at the end of this code. So let's go try that. In our controller, we call this increment item. If all goes well, it increments the product and returns to the same page. Let's see what happens. So to refresh our memory, we have three items in the cart items.
Fixing CartItem Fillables3:29
page. Let's see what happens. So to refresh our memory, we have three items in the cart items. You can see they match the ID one and two. And here I am in the incognito window from before that has a cart, it does have a cart with the ID of three. And I will go add the vanilla panakota. I'm going to click on that. And we have the same issue than before the fillable property is missing the product ID this time on the cart item. This is good. This might feel like extra steps,
the product ID this time on the cart item. This is good. This might feel like extra steps, but it is here to protect us. So once again, we're going to have a fillable property. And actually, yeah, we are going to need the product ID and quantity for sure. I don't know if we do need or want the cart ID here. Let's go with these two. And we'll try one more time, panakota add. And that shows us that the actual panakota was added to our cart. So it's working. This is going to
that shows us that the actual panakota was added to our cart. So it's working. This is going to be a surprise to no one. But when I refresh the page, we can see our new cart item entry with the cart ID of three and the timestamps added. This is pretty cool. We probably have enough backend stuff to do some UI work. But I want to do one more quick lesson on Laravel. And this is quite important because we are creating cards that will basically persist forever in our database.
Adding Cart Expiry4:43
important because we are creating cards that will basically persist forever in our database. Every time someone adds an item to the cart, very, very likely, unless you have an incredibly successful business, people will start creating a cart and then abandon it. And because this is user session ID based, they might stay there forever. So we want to do one more thing and it's to add an expiry date to all of these cards, maybe based on the updated ad field that you can see here.
an expiry date to all of these cards, maybe based on the updated ad field that you can see here. So whenever a cat has been untouched for X amount of hours or minutes or days, I don't know, we want to clear them away from the database. I'll show you how to do this in the next lesson.
