2017-10-18 96 views
0

我有問題需要將動態字段添加到數據庫中,如果我返回所有值,但是如果我嘗試添加到數據庫中,則會收到500錯誤。 這是我的充滿活力的領域:如何將動態字段添加到數據庫Lavavel

var i = 1; 
    $('#add_field').click(function(){ 
    i++; 
    $('#dynamic_field').append('<tr id="row'+i+'"><td class="col-md-5"><textarea class="name borders table-control" type="text" rows="1" cols="45" name="name[]"></textarea></td><td class="col-md-2"><input class="borders table-control price" type="text" name="price[]"></td><td class="col-md-2"><input class="borders table-control qty" type="text" name="qty[]"></td><td class="col-md-2"><input class="form-control total" type="text" name="total[]"></td><td class="text-center"><span id="'+i+'" style="color: red" name="remove" class="btn_remove"><i class="fa fa-times" aria-hidden="true"></i></span></td></tr>'); 

}); 

這是我的Ajax調用:

$(document).on('change','.qty',function(){ 


    var id = $("#inv_id").val(); 
    var tr = $(this).closest('tr'); 
    var name = tr.find(".name").val(); 
    var price = tr.find(".price").val(); 
    var qty = tr.find(".qty").val(); 
    var total = tr.find(".total").val(); 

    $.ajax({ 
     type: "POST", 
     url: '/product', 
     data: {'id': id, 'name': name, 'price': price, 'qty': qty, 'total': total, '_token':$('input[name=_token]').val()}, 
     success: function(data) { 

      console.log(data); 

     }, 
      error: function(data){ 
      alert("fail"); 

      } 
    }); 
}); 

這裏是我的控制器:

public function product(Request $request){ 

    $products = $request->all(); 

     $id = $products['id']; 
     $name = $products['name']; 
     $price = $products['price']; 
     $qty = $products['qty']; 
     $total = $products['total']; 

    for($i = 0; $i < count($name); $i++) { 


      $prod = new Product(); 
      $prod->invoice_id = $id; 
      $prod->name = $name[$i]; 
      $prod->price = $price[$i]; 
      $prod->qty = $qty[$i]; 
      $prod->total = $total[$i]; 
      $prod->save(); 

    } 


    return response()->json($prod->toArray(), 200); 

} 

這是我的產品型號

protected $fillable = [ 'invoice_id', 'name', 'qty', 'price', 'total' ]; 

I已經嘗試Product :: create($ request-> all),其實我已經嘗試了我所知道的一切。這就是爲什麼我需要你幫助球員。

+0

讓我們[繼續聊天討論](http://chat.stackoverflow.com/rooms/157032/discussion-between-ljubadr-and-denisson-de-souza)。 – ljubadr

+0

@ljubadr,我終於設法修復代碼。我的代碼很好,我只需要刷新頁面。這麼簡單,但花了很長時間。哦,我正在學習。將在這裏發佈代碼。 –

回答

0

我所要做的只是刷新頁面。

public function product(Request $request){ 
$prod = new Product(); 
$prod->invoice_id = $request->id; 
$prod->name = $request->name; 
$prod->price = $request->price; 
$prod->qty = $request->qty; 
$prod->total = $request->total; 
$prod->save(); 
return response()->json($prod->toArray(), 200); 
} 

$(document).on('change','.qty',function(){ 

    var name = []; 
    var price = []; 
    var qty = []; 
    var total = []; 


    var id = $("#inv_id").val(); 
    var tr = $(this).closest('tr'); 
    name = tr.find(".name").val(); 
    price = tr.find(".price").val(); 
    qty = tr.find(".qty").val(); 
    total = tr.find(".total").val(); 


    $.ajax({ 
     type: "POST", 
     url: '/product', 
     data: {id: id, name: name, price: price, qty: qty, total: total, '_token':$('meta[name="csrf-token"]').attr('content')}, 
     success: function(data) { 
      $('#form').load(location.href + ' #form'); 
      location.reload(); 
     } 

    }); 
}); 
相關問題