Laravel:使用 Ajax 更新数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42898877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel : Update Database using Ajax
提问by User57
I have build a shopping cart..Where I tried to update database using ajax..when I will update the quantity, it will reflect the database immediately..
我已经建立了一个购物车..我尝试使用ajax更新数据库的地方..当我更新数量时,它会立即反映数据库..
for that i used the following View:
为此,我使用了以下视图:
@foreach($carts as $row)
<input type="hidden" class="cat_id" value="{{$row->id}}"/>
<input type="number" class="quantity" value="{{$row->no_of_items}}" name="qty" maxlength="3" max="999" min="1" /> ×${{$row->p_price}}
@endforeach
Here is the Ajax Part:
这是 Ajax 部分:
$(".quantity").change(updateCart);
function updateCart(){
var qty = parseInt($(this).val());
var cat_id = parseInt($('.cat_id').val());
console.log(cat_id);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url:"{{url('cart/update/{cat_id}/{qty}')}}",
method:"POST",
data:{cat_id:cat_id, qty:qty},
success: function( data ) {
// console.log(data);
}
});
}
Route
路线
Route::post('cart/update/{cat_id}/{qty}','ProductController@cartUpdate');
Route::post('cart/update/{cat_id}/{qty}','ProductController@cartUpdate');
And the controller part :
和控制器部分:
public function cartUpdate($cat_id, $qty)
{
$cart = Cart::find($cat_id);
$product = Product::find($cart->product_id);
$cart->no_of_items = Input::get('qty');
$cart->price = $product->price * $cart->no_of_items;
$cart->save();
}
I have product_id in carts table The problem I'm facing is whenever i tried to update the Quantity i saw error on console mode:
ErrorException in ProductController.php Trying to get property of non-object
我在购物车表中有 product_id 我面临的问题是每当我尝试更新在控制台模式下看到错误的数量时:
ProductController.php 中的 ErrorException 试图获取非对象的属性
when i dd() the cat_id i see null value or same for all the curt..what could be the possible? thanks
当我 dd() cat_id 时,我看到所有内容都为空值或相同..这可能是什么?谢谢
回答by linktoahref
The issue is you are not passing the cat_id
and qty
via the url and is passed via ajax post request
问题是你是不是传递cat_id
和qty
通过URL,并通过AJAX POST请求传递
$.ajax({ url:"{{url('cart/update/{cat_id}/{qty}')}}", method:"POST", data:{ cat_id : cat_id, qty: qty }, success: function( data ) { // console.log(data); } });
hence the $cat_id
and $qty
is null
in the Controller, so you need to change the code in your controller as
因此,$cat_id
和$qty
是null
在控制器,所以你需要改变你的控制器代码
public function cartUpdate($cat_id, $qty) { $cart = Cart::find(Input::get('cat_id')); $product = Product::find($cart->product_id); $cart->no_of_items = Input::get('qty'); $cart->price = $product->price * $cart->no_of_items; $cart->save(); }
回答by V16
Try This Way:
试试这个方法:
@foreach($carts as $row)
<input type="hidden" class="cat_id" name="cat_id" value="{{$row->id}}"/>
<!--Add name="cat_id" AS Attribute -->
<input type="number" class="quantity{{$row->id}}" value="{{$row->no_of_items}}" name="qty" maxlength="3" max="999" min="1" /> ×${{$row->p_price}}
@endforeach
And Ajax Part :
和 Ajax 部分:
@foreach($carts as $row)
$(".quantity{{$row->id}}").change(updateCart);
@endforeach
function updateCart(){
var qty = parseInt($(this).val());
var cat_id = parseInt($('.cat_id').val());
console.log(cat_id);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url:'cart/update',
method:"POST",
data:{
cat_id:cat_id,
qty:qty
},
success: function( data ) {
// console.log(data);
}
});
}