I am working with stripe test payment and want to store my order in db also but facing an error i.e.
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
My controller is Checkout function
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');
}
please guide what i am doing wrong. I check almost what i can.
Because you are already getting value for foreign key
Try directly storing it like this
$order->save();
else you can also try this
Auth::user()->orders()->save($order);
instead of
Auth::user()->orders->save($order);
Refer to docs