Add to Cart Feature

Learn how to create a cart model and develop an API to add items to the cart.

Model

The model should have an association with User, Product, and quantity. We should also store createdDate, so that we can sort items in the cart. We can show the item that was added most recently at the top. But what type of relationship should it have?

Relationship

What will be the relationships between a User and a Cart? A user can have multiple cart items. So, they should have a many-to-one relationship.

Similarly, a product can be part of many carts. So, they should have a many-to-one relationship.

We’ll define the model as follows. It will also have a constructor.

Get hands-on with 1200+ tech skills courses.