2016-12-30 12 views
2

私が必要とする答えはどこかであると信じていますが、どういうわけか私のコードで何が問題なのか分かりません。SQLSTATE [23000]:整合性制約違反:1048. vueからlaravelコントローラへのデータの投稿

注文を作成した後、注文明細テーブルを作成しようとしています。 したがって、vueからデータをエンドポイントに送信します。

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 = {}; 
     }); 

はその後、私はデータを収集したいコントローラの店舗方法では、カート内のすべてのアイテムを使用してORDER_DETAILSテーブルを作成し、順序を作成します。

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); 

} 

トレースバックでは、すべての値がNULLを返します。したがって、私が得ているdbエラーです。私の賭けは、実際にアイテムを手に入れたり、プロパティに正しくアクセスしていないということです。

ここは、役立つ場合はペイロードのショットです。私はまた、データを投稿するためのより良い方法の提案にもオープンしているので、私は両方のフレームワークには新しいので、使いやすくなっています。

enter image description here

エラーメッセージ:id_products

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)) 
+0

すべての商品にproduct_idがあることを確認します。 –

+0

また、 '$ items = $ request-> input( 'items');で取得したものを共有します。 – C2486

答えて

3

値が変数$アイテムにここに来ていません。

あなたは既存の列を変更するには、デフォルト値に

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

を置くべきid_products少なくとも空白

OR

の価値を提供する必要が

$items = $request->items; 

で試してみてくださいあなたSEこの

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

あなたは、私は、各オブジェクトのプロパティをこのようにアクセスしようとしたし、それが働いたlaravel 5.3

+0

そうなら、商品がなくても注文詳細を提出できます。 –

+0

あなたが提案した$ request-> itemsを使ってみましたが、同じエラーです。 –

+0

'$ request - > $ request'データを' store'メソッドで共有してください。 – C2486

0

内の任意のデフォルト値なしにこの空白を維持することはできません。

public function store(Request $request) 
{ 
    $items = $request->items; 

    foreach ($items as $item){ 
     $order_detail = new OrderDetail(); 
     $order_detail->order_id = 99; 
     $order_detail->id_products = $item['product_id']; 
     $order_detail->quantity = $item['quantity']; 
     $order_detail->price = $item['price'] * $item['quantity']; 

     $order_detail->save(); 
    } 
    return response("success"); 


} 

なぜそうだと思いますか?

関連する問題