I'm building API using Laravel.
In this API, I use two models, Order
model and Product
model.
One order can have many products. In order to make this relationship, I made 3 tables.
Below are the tables:
orders
Field Type
id INT
user_id INT
created_at TIMESTAMP
updated_at TIMESTAMP
products
Field Type
id INT
name VARCHAR
price DECIMAL
created_at TIMESTAMP
updated_at TIMESTAMP
order_items
Field Type
id INT
order_id INT
product_id INT
quantity INT
created_at TIMESTAMP
updated_at TIMESTAMP
The question is, if the API client has an order page (or you can say cart page), when the user submit the cart form, the client will post multiple order items (products) to the server, knowing this,
- How do we put the multiple order items (products) in the AJAX JSON data?
- What Route URL is right for this scenario if we are using REST?
- How do we handle the json data that contains multiple order items in the controller?