美好的一天,我正在为我的学习做推车订单服务 API 。我正在努力解决如何检查多件物品的问题。
第一个问题
我不知道如何将一个数组(像下面的输出一样)从post请求传递给我的控制器或方法。用户完成选择他/她的项目后。
第二
如何实现数组(类似于下面的输出)
当用户1234选择1项时,输出将如下所示。
Array ( [user_id] => 1234
[0] => Array (
[product_id] => 123
[product_name] => sample product 1
[product_desc] => A sample product
[product_price] => 100
)
)
当用户1234选择2个项目时。这将是输出。
Array ( [user_id] => 1234
[0] => Array (
[product_id] => 1
[product_name] => sample product 1
[product_desc] => A sample product
[product_price] => 100
)
[1] => Array (
[product_id] => 2
[product_name] => sample product 2
[product_desc] => A sample product
[product_price] => 100
)
)
当用户12选择2个项目时。这将是输出。
Array ( [user_id] => 12
[0] => Array (
[product_id] => 1
[product_name] => sample product 2
[product_desc] => A sample product
[product_price] => 100
)
[1] => Array (
[product_id] => 2
[product_name] => sample product 2
[product_desc] => A sample product
[product_price] => 200
)
)
很快......
已编辑这是我的测试结帐表单。目前它只能一次结账单项。
<form action="/test/api/checkoutItem" method="POST">
<input type="hidden" name="cart_items_id" id="cart_items_id" value="55">
<input type="hidden" name="user_id" id="user_id" value="11647748">
<input type="hidden" name="product_id" id="product_id" value="70">
<input type="input" name="qty" id="qty">
</form>
控制器
public function checkoutItem(Request $request)
{
$id = $request->user_id;
//Status TESTED AND OK.
try{
//Check if cart has an item whith status id 1 (Added).
$cart = \DB::table('cart_items')
->whereRaw('cart_items.id = ' .$request->cart_items_id.
' AND cart_items.product_id = ' .$request->product_id.
' AND cart_items.status_id = 1')->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->product_id);
//Check if student's has no stripe id.
if($student->stripe_id == NULL){
//create stripe id for student.
$customer= Customer::create([
'email' => request('stripeEmail'),
'source' => request('stripeToken')
]);
//Update stripe_id from null to newly created stripe_id generated by stripe.
$user= \App\User::where('id', $id)
->update( array('stripe_id' => $customer->id) );
//Charge the student.
Charge::create([
'customer' => $customer->id,
'description' => $product->description,
'amount' => ($product->price * 100) * $request->qty, //Should be in cent.
'currency' => 'aud',
'receipt_email' => $request->stripeEmail,
'metadata' => array(
"product_id" => $request->product_id,
"product_name" => $product->name,
"product qty" => $request->qty,
"product_type_id" => $product->product_type_id,
"sub_type_id" => $product->service_sub_type_id,
"user_id" => $id,
"fullname" => $student->first_name." ".$student->last_name,
"email" => $request->stripeEmail
)
]);
//If student's has already stripe id, create a charge.
}else{
//Charge
Charge::create([
'customer' => $student->stripe_id,
'description' => $product->description,
'amount' => ($product->price * 100) * $request->qty, //Should be in cent.
'currency' => 'aud',
'receipt_email' => $student->email,
'metadata' => array(
"product_id" => $request->product_id,
"product_name" => $product->name,
"product qty" => $request->qty,
"product_type_id" => $product->product_type_id,
"sub_type_id" => $product->service_sub_type_id,
"user_id" => $id,
"fullname" => $student->first_name." ".$student->last_name,
"email" => $student->email
)
]);
}
//If successfully charge, status_id wll be updated from 1(Added) to 2(Checkout).
$upd_cart_items= \DB::table('cart_items')
->leftjoin('carts', 'cart_items.cart_id', '=', 'carts.id')
->whereRaw('cart_items.id = ' .$request->cart_items_id)
->update(['cart_items.quantity' => $request->qty,'cart_items.status_id' => 2,
'cart_items.updated_at' => Carbon::now()->toDateTimeString()]);
return ['success' => true, 'message' => "Product was successfully purchased"];
}
}catch(\Stripe\Error\Card $e){
return['error' => $e];
}
}
很抱歉这个愚蠢的问题。
但有人说:
提出问题的人是五分钟的傻瓜;不问问题的人永远是个傻瓜
答案 0 :(得分:0)
PHP处理POST和GET变量的一个特点就是它 自动解码索引形式的变量名称。
你可以这样做
<form ....>
<input name="person[userId]" value="1234" />
<input name="person[0][product_id]" value="1" />
<input name="person[0][product_name]" value="sample product 1" />
...
<input name="person[1][product_id]" value="2" />
<input name="person[1][product_name]" value="sample product 2" />
</form>
在php中,您可以通过以下方式检索它:
<?php
var_dump($_POST['person']);
//will get you something like:
[user_id] => 1234
[0] => Array (
[product_id] => 1
[product_name] => sample product 1
)
[1] => Array (
[product_id] => 2
[product_name] => sample product 2
)
?>