dongqian3750 2018-10-08 09:25
浏览 505

Laravel API,在一个POST请求中传递多个对象的正确方法

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,

  1. How do we put the multiple order items (products) in the AJAX JSON data?
  2. What Route URL is right for this scenario if we are using REST?
  3. How do we handle the json data that contains multiple order items in the controller?
  • 写回答

2条回答 默认 最新

  • dtcd27183 2018-10-08 09:41
    关注

    So based on you questions.. The following json will work.. there will be array of order 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 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 a example to let you how you can use it.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大