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

Cart Entities Overview0:00

We are back on the Laravel train and we're going to implement the backend for the shopping cart Okay, so we've built the design for the empty cart state And now we want to tackle what happens when we actually have products in our cart all of these good stuff So let's try to think in terms of database models or entities for this shopping cart We have of course a cart which is going to contain the product, but then if you look through the cart There is these line items or cart items

look through the cart There is these line items or cart items And I think we're going to need two entities a cart and the cart item And then these are going to be related to each other We are not going to have real users in this application or this challenge And so we're going to attach the shopping cart to the session ID Every time someone visits the website there is a session and we can use the session ID to attach the cart Now we're going to try to be clever about this We don't want to create a new cart on every page visit

Creating Cart Model0:55

Now we're going to try to be clever about this We don't want to create a new cart on every page visit But only instantiate the cart whenever the first item is placed into the cart But let's not get ahead of ourselves. Let's start by creating the cart model and database migration PHP Artisan make model This model is the cart and we don't need a cedar or a factory really all we need here is the migration So that's going to create two files and we'll start by setting the correct database fields on the cart's table

So that's going to create two files and we'll start by setting the correct database fields on the cart's table Okay, and like I mentioned here We're going to keep the ID and timestamps and we are going to add a column Which is a string and is going to be the session ID I think that's all we need for our cart's table and then we're going to create our cart item model and database table so that a cart can have multiple cart items and A cart item can belong to a cart a cart has many cart items and a cart item belongs to a cart

Creating Cart Item Model1:53

A cart item can belong to a cart a cart has many cart items and a cart item belongs to a cart This sounds like the relationships that we need to set up all right PHP Artisan make model one more time this time cart item and here again, we just need the migration and Let's jump into the table columns definition now have a thing for a second What should a cart item consist of or be connected to to me? It should be connected to a product The user is going to take a product and place it in the cart as a cart item And so the other connection obviously is the cart so we have a product

The user is going to take a product and place it in the cart as a cart item And so the other connection obviously is the cart so we have a product We have a cart and the cart item is going to be the link between the two basically So a cart items table needs reference to product and cart database table entries So what we want here is a foreign ID and I like to use foreign ID for and then I can pass the model class So product class and so we want to make sure that this is constrained and whenever the related product is deleted

So product class and so we want to make sure that this is constrained and whenever the related product is deleted We should also delete the cart item. So we are going to cascade on delete So that's the relationship with the product and we're going to do exactly the same for the cart pretty much that Yes, but I would like to be consistent and use foreign ID for and here use the cart Class. Oh my god. So much VS code overlays. It's confusing There's one more bit of information that the cart item should know about and it 's the item quantity

There's one more bit of information that the cart item should know about and it 's the item quantity If you look at the design you can see that each line item has 1x 4x 2x So we need to know the quantity for each cart item I guess this can be an unsigned integer And I want to do one more quite important thing and it's to make sure that the pairing of a product and a cart Only occurs once and is unique. So after the timestamps, I'll add a Yes, copilot. Thank you. You heard me a unique constraints that make sure that we never have a

Defining Model Relationships3:57

Yes, copilot. Thank you. You heard me a unique constraints that make sure that we never have a Duplicate pair of product ID and cart ID, which is these two relationships here All right I think that we good with our database tables and rows and we've set up the relationship at the database level But now we need to go in the models and also configure the relationship there So if I go to my cart model remember that a cart can have multiple items, so public function items and this is going to be a

remember that a cart can have multiple items, so public function items and this is going to be a Relationship like I said before a cart can has many it can has many items And so we are going to return this has many cart item class this sets up the has many relationship between a cart and Multiple cart items and we'll do the counterpart for this cart item Model and here we are going to let copilot tell us P for public. Okay. I'll do it myself fine public function cart This time a cart item

Running Migrations5:00

public function cart This time a cart item belongs to a Cart and so the relationship is this belongs to the cart class Okay, we're making progress before we forget. Let's run the database migrations PHP artisan Migrate and this should run two migrations the cards and card items tables nice To wrap up this lesson Let's go in table plus and I will refresh the database and you can see our cards and card items tables created great

Testing Data Retrieval5:26

Let's go in table plus and I will refresh the database and you can see our cards and card items tables created great So let's create a dummy card I'll have a random session ID string and we'll create a couple of card items So if I look at the products the ID range from one to nine So let's say we want to add one waffle and two classic tiramisu to our cart So this is ID one and four our cart has an idea of one So I will create my first card item This is going to be the waffle the product ID is one the cart ID is one and the quantity is one and

This is going to be the waffle the product ID is one the cart ID is one and the quantity is one and Let's create a second one. The product ID is for the tiramisu the cart ID is still one and the quantity is two And we're going to save this to the database now Let's see really quickly if we can pull this data inside our front end So I'll go in web that PHP and we have products But let's also try to retrieve the card So cut now remember that the cards are going to use the session ID to be identified

So cut now remember that the cards are going to use the session ID to be identified Here we'll just retrieve the first card just for demo purposes So maybe let's go first or fail and then in our welcome that blades that PHP templates Just under the heading here. Let's dump and die The cart but let's try to drill down to the cart items another note here We should ego load the items and we will but now this is just a quick hello world Can we get the cart items on the screen and the answer is yes, we can here's

world Can we get the cart items on the screen and the answer is yes, we can here's the first card item You can see we can retrieve the data for it. And so we could add for each cart items as item display like copilot suggest the item product name and the item quantity Moment of truth. Can we pull this off? Haha, we cannot and I know why do you know why? Well, the answer is here The item product is null because we haven't set the product relationship on the card item

The item product is null because we haven't set the product relationship on the card item If I go in the cart item model remember, we've set the cart belongs to relationship But a card item also technically belongs to a product exactly like that So with this relationship in place Here we go. It's working. We have a one waffle and two tiramisu's in the cart so There's still plenty of work to do on the back end We need to implement the add to cart functionality the remove from cart

There's still plenty of work to do on the back end We need to implement the add to cart functionality the remove from cart increment quantity decrement quantity clear the whole cart and all that sort of stuff I think we're going to tend them between UI and back end at the same time I might even wear both t-shirts at the same time in one lesson. Who knows? But for now, let's wrap up this lesson before it gets too long and continue in the next one

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