简体   繁体   English

Laravel:使用Ajax更新数据库

[英]Laravel : Update Database using Ajax

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" /> &times;${{$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'); 路线::后( '车/更新/ {CAT_ID} / {}数量', '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: 我的购物车表中有product_id我面临的问题是,每当我尝试更新控制台模式下看到的错误数量时:

    ErrorException in ProductController.php Trying to get property of non-object ProductController.php中的ErrorException试图获取非对象的属性

when i dd() the cat_id i see null value or same for all the curt..what could be the possible? 当我dd()cat_id时,我看到所有curt的值为null或相同。.可能是什么? thanks 谢谢

The issue is you are not passing the cat_id and qty via the url and is passed via ajax post request 问题是您没有通过URL传递cat_idqty ,而是通过ajax发布请求传递

$.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();
    }

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" /> &times;${{$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);
                    }
                   });
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM