تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Session-Based Cart Ownership0:00

We've got a cart model and database table, but we cannot really do anything with it at the moment. Let's change this. Okay, so as a proof of concept in the previous lesson, we created manually a fake cart and added some cart items to it. And then we passed the first card here to our front end. So our app will potentially have multiple cards, and we definitely don't want to just pass the first card to every user. Remember, like I mentioned, we want to use the session ID to identify if a cart belongs to the current visitor.

Adding Cart Lookup Method0:29

Remember, like I mentioned, we want to use the session ID to identify if a cart belongs to the current visitor. Let's start implementing this. In our cart model, we are going to add some functionality. Our app won't necessarily have or need a cart, so we first need to have a function that checks if a cart exists. So I'll have public static function, and let me call this if exists. And here we'll check if we can find a cart that has the session ID matching the current session ID. So return static column column.

current session ID. So return static column column. And let's do some eager loading while we here. So I'll use the with relationship loader. And as co-pilot suggests, we're going to eager load the items, and then the product within each item. Remember, a relationship setup, a cart has many cart items, and then a cart item belongs to a product. And so whenever we retrieve the cart, we will want to have this information about the cart items and the product.

And so whenever we retrieve the cart, we will want to have this information about the cart items and the product. So let's eager load these within the cart query. So we don't have to do multiple extra queries for each items within the cart. And at this point, we exactly back to where we were getting the first cart, which we don't want. So before the first, we actually want to have a wear condition, and we're going to check for the session ID property here should match the session get ID, which is how we get the ID of the current session.

session get ID, which is how we get the ID of the current session. So this is either going to return a cart instance or now. And so now in web.php, instead of retrieving the first cart, we can do this check and get the cart if it exists. And interesting, you can see here that co-pilot suggests that if the cart doesn 't exist, we should create a new cart. We do not want to do that. Think about it.

We do not want to do that. Think about it. This will run at every page visits. Whenever someone visits the welcome page, this is going to run. And we don't really need a shopping cart until someone actually adds a product to the cart. This is important because any random spam visits or any person visiting the site would create a new cart instance in the database. And it's probably going to fill up the DB with a lot of junk that we don't need

Avoiding Unnecessary Cart Creation2:40

would create a new cart instance in the database. And it's probably going to fill up the DB with a lot of junk that we don't need . Instead of that happening, we can be clever here. And for UI purposes, we just check if a cart exists. If it doesn't, and we get null, we can just display the empty cart. But we only want to really create a cart when the user adds an item to the cart for the first time. So we're going to have this if exists convenience for the front end, mostly. And then we're going to have another one and sure exists, I guess,

So we're going to have this if exists convenience for the front end, mostly. And then we're going to have another one and sure exists, I guess, that is actually making sure that we do have a cart before we add an item to it . So this is important to have this nuance. This is just preventing a lot of unnecessary carts to be created in the database, which I think is a good idea. So here I will politely decline the suggestion from co-pilot. And let's go check out our front end with this in place.

Handling Null Cart in UI3:26

So here I will politely decline the suggestion from co-pilot. And let's go check out our front end with this in place. Okay, we seem to have an error, which is actually good. You can see that we were looping over items when picking up the first cart. But now that the cart is actually null, we have a problem. Remember, in the last lesson, we had created this loop on the welcome template. And essentially here, we should just check if we have a cart before running this code. And our UI is working once again. So it looks like we do not have a cart currently.

Testing Session ID Matching3:59

And our UI is working once again. So it looks like we do not have a cart currently. And well, that makes sense. I don't know if you remember, but when we created the cart in the database, we just had a random string, gibberish string for the session ID. Obviously, the likelihood of this matching the current visitors session ID is pretty low. Let's try something here. So in our if exist method that we've created in our cart model, let's dump and die the session ID.

So in our if exist method that we've created in our cart model, let's dump and die the session ID. Here I'll go didi session get ID. And let's refresh the page. And we are going to get this session ID, the actual session ID. So I'll copy this. And in our database, in the existing carts, let's go update the session ID field with that session ID. I'll hit save. So now the visitors session ID and the cart session ID should match.

I'll hit save. So now the visitors session ID and the cart session ID should match. And I'm hoping that that means that we get a cart returned in the if exist function. Let's remove the dump and die here and cross our fingers and refresh the home page. Very cool. This time we have a match with that setup in place. Each different visitor should be able to have its own cart. Actually, let me visit the same URL in incognito mode.

Each different visitor should be able to have its own cart. Actually, let me visit the same URL in incognito mode. As you can see, we don't see any cart items, because the existing cart in the database doesn't match this user's session ID. Let me bring back the dump and die here. And I will get the session ID for this user, copy it. And in the database, I will create a second cart with that session ID. So you can see the cart ID of two. So let's add a cart item. This time we'll add the ID three.

So let's add a cart item. This time we'll add the ID three. And importantly, we'll set the cart ID to two, a new cart. Let's add 10 of them and hit save. I will once again remove the dump and die from our if exist function. And here, when I refresh the page, I'm expecting to see 10 entries for whatever dessert we've chosen. Ready? Three, two, one, refresh. And there we are.

Three, two, one, refresh. And there we are. We have 10 macaron mix of five. And the really cool thing here is if I go back to the other session for the other user, they should still have their cart with one dessert and two desserts with the tiramisu and the creme brulee that we had added to the other cart. And you can see it right there, except it's not the creme brulee, but the w affles. It's working.

Planning Cart Creation Logic6:22

affles. It's working. Okay, so we're able to retrieve a cart based on the session ID now. So the next logical step is to add functionality to create a cart based on the session ID, instead of doing it manually, like we've done in this lesson. Let's do that next.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟