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:
Field Type
id INT
user_id INT
created_at TIMESTAMP
updated_at TIMESTAMP
Field Type
id INT
name VARCHAR
price DECIMAL
created_at TIMESTAMP
updated_at TIMESTAMP
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,
So based on your questions. The following JSON will work. there will be an array of orders inside JSON which will have multiple orders Like below:
{
"user_id": 1
"orders": [
{"product_name": "Whatever1 is selected", "quantity": 1},
{"product_name": "Whatever2 is selected", "quantity": 2},
{"product_name": "Whatever3 is selected", "quantity": 3},
],
}
Then on server side:
public function store(Request $request)
{
// after validating this you can use foreach to parse the the json
foreach($request->orders as $order)
{
//suposse you have orders table which has user id
Order::create([
"product_name" => $order['product_name'],
"quantity" => $order['quantity'],
"user_id" => $request->user_id // since this is just json object not an jsonarray
]);
}
}
if you are using Laravel Passport then you don't need to specify the user_id in JSON. in this case, your JSON will be like:
{
"orders": [
{"product_name": "Whatever1 is selected", "quantity": 1},
{"product_name": "Whatever2 is selected", "quantity": 2},
{"product_name": "Whatever3 is selected", "quantity": 3},
],
}
Then on server side in you controller:
public function store(Request $request)
{
// after validating this you can use foreach to parse the the json
foreach($request->orders as $order)
{
//suposse you have orders table which has user id
Order::create([
"product_name" => $order['product_name'],
"quantity" => $order['quantity'],
"user_id" => Auth::id() // since you are using passport
]);
}
}
Route inside api.php file:
Route::post('user/order','OrderController@store');
// if using Laravel Passport
Route::post('user/order','OrderController@store')->middleware('auth:api');
This is how you can store multiple orders of the same user in a JSON using passport and without passport package.
Note: you can changes json key names according to your design.. This is just an example to let you how you can use it.
When it comes to an Orders with Products API I always ask myself, "What Would Shopify Do?" (WWSD)
This link documents their orders api and shows you how to post an order with products in json and the json response you get back.
https://help.shopify.com/en/api/reference/orders/order#create
The page also has examples of close, cancel, update, delete, etc.
But be warned, there's a lot of tables being updated behind the API besides Order and Product.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With