Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel API, the right way to pass multiple objects in one POST request

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?
like image 802
Edward Anthony Avatar asked Mar 06 '23 06:03

Edward Anthony


2 Answers

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.

like image 79
Salman Zafar Avatar answered Mar 12 '23 16:03

Salman Zafar


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.

like image 39
ourmandave Avatar answered Mar 12 '23 16:03

ourmandave