我正在使用条带测试付款,并希望在db中存储我的订单,但面临错误,即
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
我的控制器是结帐功能
public function postCheckOut(Request $request){
if(!Session::has('cart')){
return redirect()->route('shop.cart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
/* Stripe Test Api key */
Stripe::setApiKey('-------');
try{
$charge = \Stripe\Charge::create(array(
"amount" => $cart->totalPrice * 100,
"currency" => "usd",
// "source" => "$request->input('stripeToken')", // obtained with Stripe.js
'card' => array(
'number' => $request->get("cnumber"),
'exp_month' => $request->get("exp-month"),
'exp_year' => $request->get("exp-year"),
'cvc' => $request->get("cvc"),
),
"description" => "product purchased"
));
/* Saving order data after purchasing done */
$order = new Order();
$order->user_id = Auth::id();
$order->cart = serialize($cart);
$order->address = $request->input('address');
$order->name = $request->input('name');
$order->payment_id = $charge->id;
// dd($order);exit;
/* Saving order data by orm relation calling login user then its order function and then save orders */
Auth::user()->orders->save($order);
} catch (\Exception $e){
return redirect()->route('checkout')->with('error', $e->getMessage());
}
Session::forget('cart');
return redirect()->route('product.index')->with('success', 'Product purchased');
}
请指导我做错了什么。我几乎检查了我能做什么。
答案 0 :(得分:0)
因为您已经获得了foreign key
的价值,请尝试直接存储它
$order->save();
否则你也可以试试这个
Auth::user()->orders()->save($order);
而不是
Auth::user()->orders->save($order);
请参阅docs