admin管理员组文章数量:1312922
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?
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?
2 Answers
Reset to default 6So 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 es 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./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.
本文标签: javascriptLaravel APIthe right way to pass multiple objects in one POST requestStack Overflow
版权声明:本文标题:javascript - Laravel API, the right way to pass multiple objects in one POST request - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741829005a2399819.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论