【问题标题】:Laravel API, the right way to pass multiple objects in one POST requestLaravel API,在一个 POST 请求中传递多个对象的正确方法
【发布时间】:2019-03-12 22:35:54
【问题描述】:

我正在使用 Laravel 构建 API。

在这个 API 中,我使用了两个模型,Order 模型和 Product 模型。

一个订单可以有很多产品。为了建立这种关系,我做了 3 个表。

下表是:

订单

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

order_items

Field       Type  
id          INT
order_id    INT
product_id  INT
quantity    INT
created_at  TIMESTAMP
updated_at  TIMESTAMP

问题是,如果API客户端有一个订单页面(或者你可以说购物车页面),当用户提交购物车表单时,客户端会发布多个订单商品(产品)到服务器知道这一点,

  1. 我们如何将多个订单项(产品)放入 AJAX JSON 数据中?
  2. 如果我们使用 REST,什么路由 URL 适合这种情况?
  3. 我们如何处理控制器中包含多个订单项的json数据?

【问题讨论】:

    标签: javascript php laravel api-design


    【解决方案1】:

    所以根据您的问题。 以下 JSON 将起作用。 JSON 中会有一个订单数组,其中有多个订单,如下所示:

    {
        "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},
        ],
    }
    

    然后在服务器端:

    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
          ]);
       }
    }
    

    如果你使用 Laravel Passport,那么你不需要在 JSON 中指定 user_id。在这种情况下,您的 JSON 将如下所示:

      {
            "orders": [
                {"product_name": "Whatever1 is selected", "quantity": 1},
                {"product_name": "Whatever2 is selected", "quantity": 2},
                {"product_name": "Whatever3 is selected", "quantity": 3},
                ],
        }
    

    然后在你的控制器中的服务器端:

    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
          ]);
       }
    }
    

    api.php 文件中的路由:

    Route::post('user/order','OrderController@store');
    
    // if using Laravel Passport
    Route::post('user/order','OrderController@store')->middleware('auth:api');
    

    这就是您可以使用护照而不使用护照包将同一用户的多个订单存储在 JSON 中的方法。

    注意:您可以根据自己的设计更改 json 键名。 这只是一个示例,让您了解如何使用它。

    【讨论】:

    • 感谢您的回复。用户无法指定产品的价格,所以我认为传递 items: [{ "product_id": 1, "quantity": 3 }] 之类的内容就足够了。对于用户 ID,我可以从授权标头中传递的令牌中获取它,并且要获取当前用户,我可以简单地执行 Auth::user()
    • @EdwardAnthony 我告诉过你这只是一个例子。你可以根据数量改变价格,如果你使用的是护照,那么你也可以删除用户 ID 而不是你可以使用 Auth::id()或 Auth::user()->id
    • @EdwardAnthony 我已经更新了我对 laravel 护照案的回答
    【解决方案2】:

    谈到 Orders with Products API 时,我总是问自己:“Shopify 会做什么?” (WWSD)

    此链接记录了他们的订单 api,并向您展示了如何使用 json 格式的产品发布订单以及您返回的 json 响应。

    https://help.shopify.com/en/api/reference/orders/order#create

    页面还有关闭、取消、更新、删除等示例

    但请注意,除了 Order 和 Product 之外,API 后面还有很多表正在更新。

    【讨论】:

    • 我喜欢“Shopify 会做什么”这句话。是的,他们的 API 设计得很好。
    猜你喜欢
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 2019-08-03
    • 1970-01-01
    • 2017-11-11
    相关资源
    最近更新 更多