I believe the answer to what I need is somewhere here but I somehow can't figure out what the problem is with my code.
I am trying to create an order details table after creating an order. So I post the data to the endpoint from vue here.
createOrder: function() {
this.$http.post('/api/orders', {
customer_id: this.form.customer.id,
items: _.map(this.cart, function(cart){
return {
//these are the only values I need from the each item in the cart.
product_id: cart.id,
quantity: cart.quantity,
price: cart.price
}
})
}).then(function(response) {
let responseBody = response.body;
this.$set('cart', []);
this.form.customer = {};
});
then in the store method of the controller I want to collect the data, create an order, the create an order_details table using all the items in the cart.
public function store(Request $request)
{
$items = $request->input('items');
$staff = $request->user()->id;
$customer = $request->input('customer_id');
$status_id = 1;
$order = new Order();
$order->user_id = Auth::user()->id;
$order->id_statuses = $status_id;
$order->id_customers = $customer;
//save order
$order->save();
foreach($items as $item){
$order_detail = new OrderDetail();
$order_detail->order_id = $order->id;
$order_detail->id_products = $item->product_id;
$order_detail->quantity = $item->quantity;
$order_detail->price = $item->price;
//save order detail
$order_detail->save();
}
return response()->json($order);
}
In the traceback, all the values return null hence the db error I'm getting. My bet is I am not actually getting the items or properly accessing there properties.
Here is a shot of the payload if it helps. I'm also open to suggestions of better ways of posting the data so it's easier to work with as I am new to both these frameworks.
ERROR MESSAGE:
SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'id_products' cannot be null (SQL: insert into `order_details`
(`order_id`, `id_products`, `quantity`, `price`, `updated_at`,
`created_at`) values (78, , , , 2016-12-30 17:18:04, 2016-12-30 17:18:04))