title: Array: how to achieved this kind of array
body: <p>Good day, I'm doing a cart order service <strong>API</strong> for my study. and I was struggling with how to do multiple items check out.</p>
  
  <p>First problem</p>
  
  <blockquote>
    <p>I don't know how to pass an array(<em>Like the output below</em>) from post request to my controller or method. After the user is done selecting his/her item(s).</p>
  </blockquote>
  
  <p>Second </p>
  
  <blockquote>
    <p>How can I achieve an array(<em>Like the output below</em>)</p>
  </blockquote>
  
  <p>When user 1234 selects 1 item the output will look like this.</p>
  
  <pre><code>Array ( [user_id] =&gt; 1234 
          [0] =&gt; Array ( 
                          [product_id]    =&gt; 123 
                          [product_name]  =&gt; sample product 1 
                          [product_desc]  =&gt; A sample product 
                          [product_price] =&gt; 100 
                      ) 
          )
  </code></pre>
  
  <p>When user 1234 selects 2 items. this will be the output.</p>
  
  <pre><code>Array ( [user_id] =&gt; 1234 
          [0] =&gt; Array ( 
                          [product_id]    =&gt; 1 
                          [product_name]  =&gt; sample product 1 
                          [product_desc]  =&gt; A sample product 
                          [product_price] =&gt; 100 
                      ) 
          [1] =&gt; Array ( 
                          [product_id]    =&gt; 2 
                          [product_name]  =&gt; sample product 2 
                          [product_desc]  =&gt; A sample product 
                          [product_price] =&gt; 100 
                      ) 
          )
  </code></pre>
  
  <p>When user 12 selects 2 items. this will be the output.</p>
  
  <pre><code>Array ( [user_id] =&gt; 12 
          [0] =&gt; Array ( 
                          [product_id]    =&gt; 1
                          [product_name]  =&gt; sample product 2 
                          [product_desc]  =&gt; A sample product 
                          [product_price] =&gt; 100 
                      ) 
          [1] =&gt; Array ( 
                          [product_id]    =&gt; 2 
                          [product_name]  =&gt; sample product 2 
                          [product_desc]  =&gt; A sample product 
                          [product_price] =&gt; 200 
                      ) 
          )
  </code></pre>
  
  <p>and soon...</p>
  
  <p><strong>Edited</strong> this is my test checkout form. For now it can only checkout single item at a time.</p>
  
  <pre><code>&lt;form action="/test/api/checkoutItem" method="POST"&gt;
      &lt;input type="hidden" name="cart_items_id" id="cart_items_id" value="55"&gt;
      &lt;input type="hidden" name="user_id" id="user_id" value="11647748"&gt;
      &lt;input type="hidden" name="product_id" id="product_id" value="70"&gt;
      &lt;input type="input"  name="qty" id="qty"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public function checkoutItem(Request $request)
  { 
      $id     = $request-&gt;user_id;
      //Status TESTED AND OK.
      try{
          //Check if cart has an item whith status id 1 (Added).
          $cart = \DB::table('cart_items')
                  -&gt;whereRaw('cart_items.id = ' .$request-&gt;cart_items_id. 
                      ' AND cart_items.product_id = ' .$request-&gt;product_id. 
                      ' AND cart_items.status_id = 1')-&gt;count();
  
          if($cart != NULL){
  
              Stripe::setApiKey( config('services.stripe.secret') );
              $student= \App\User::find($id);
  
              //Get the product details using the product_id
              $product= \App\Product::find($request-&gt;product_id);
  
              //Check if student's has no stripe id.
              if($student-&gt;stripe_id == NULL){
                  //create stripe id for student.
                  $customer= Customer::create([
                                              'email'    =&gt; request('stripeEmail'),
                                              'source'   =&gt; request('stripeToken')
                                              ]);
                  //Update stripe_id from null to newly created stripe_id generated by stripe.
                  $user= \App\User::where('id', $id)
                  -&gt;update( array('stripe_id' =&gt; $customer-&gt;id) ); 
  
                  //Charge the student.
                  Charge::create([
                      'customer'      =&gt; $customer-&gt;id,
                      'description'   =&gt; $product-&gt;description,
                      'amount'        =&gt; ($product-&gt;price * 100) * $request-&gt;qty, //Should be in cent.
                      'currency'      =&gt; 'aud',
                      'receipt_email' =&gt; $request-&gt;stripeEmail,
                      'metadata'      =&gt; array(
                                              "product_id"        =&gt; $request-&gt;product_id, 
                                              "product_name"      =&gt; $product-&gt;name,
                                              "product qty"       =&gt; $request-&gt;qty,
                                              "product_type_id"   =&gt; $product-&gt;product_type_id,
                                              "sub_type_id"       =&gt; $product-&gt;service_sub_type_id,
                                              "user_id"           =&gt; $id,
                                              "fullname"          =&gt; $student-&gt;first_name." ".$student-&gt;last_name,
                                              "email"             =&gt; $request-&gt;stripeEmail
                                              )
                      ]);
              //If student's has already stripe id, create a charge.   
              }else{
                  //Charge
                  Charge::create([
                      'customer'      =&gt; $student-&gt;stripe_id,
                      'description'   =&gt; $product-&gt;description,
                      'amount'        =&gt; ($product-&gt;price * 100) * $request-&gt;qty, //Should be in cent.
                      'currency'      =&gt; 'aud',
                      'receipt_email' =&gt; $student-&gt;email,
                      'metadata'      =&gt; array(
                                              "product_id"        =&gt; $request-&gt;product_id, 
                                              "product_name"      =&gt; $product-&gt;name,
                                              "product qty"       =&gt; $request-&gt;qty,
                                              "product_type_id"   =&gt; $product-&gt;product_type_id,
                                              "sub_type_id"       =&gt; $product-&gt;service_sub_type_id,
                                              "user_id"           =&gt; $id,
                                              "fullname"          =&gt; $student-&gt;first_name." ".$student-&gt;last_name,
                                              "email"             =&gt; $student-&gt;email
                                              )
                      ]); 
              }
  
              //If successfully charge, status_id wll be updated from 1(Added) to 2(Checkout).
              $upd_cart_items= \DB::table('cart_items')
              -&gt;leftjoin('carts', 'cart_items.cart_id', '=', 'carts.id')
              -&gt;whereRaw('cart_items.id = ' .$request-&gt;cart_items_id)
              -&gt;update(['cart_items.quantity' =&gt; $request-&gt;qty,'cart_items.status_id' =&gt; 2, 
                        'cart_items.updated_at' =&gt; Carbon::now()-&gt;toDateTimeString()]);
  
              return ['success' =&gt; true, 'message' =&gt; "Product was successfully purchased"];
  
          }
      }catch(\Stripe\Error\Card $e){
          return['error' =&gt; $e];
      }
  }
  </code></pre>
  
  <p>sorry for asking this foolish question.</p>
  
  <p>But someone said: </p>
  
  <blockquote>
    <p>He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever</p>
  </blockquote>