drkenap147751 2016-12-30 12:23
浏览 181
已采纳

SQLSTATE [23000]:完整性约束违规:1048。将数据从vue发布到laravel控制器

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.

enter image description here

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))
  • 写回答

2条回答 默认 最新

  • doudou32012 2016-12-30 12:32
    关注

    Value of id_products is not coming here in $items variable.

    Try with

    $items = $request->items;
    

    You need to provide value of id_products at least blank

    OR

    You should put default value

    $table->integer('id_products')->default(0);
    

    To change existing column use this

    $table->integer('id_products')->default(0)->change();
    

    You can't keep this blank without any default value in laravel 5.3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line