2016-12-26 138 views
-1

有一个这样的形式。这是一个更新表单,我只需要在购物车中更新qty foreach产品。 但我试图爆炸每个结果,不工作...它返回object2array转换错误...它第一次,我得到这个错误我怎么能保存在数据库中?购物车更新laravel

​​

这是我的控制器:

Route::post('aggiorna', function() { 
$quantita = Input::all(); 
$carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get(); 
    $quantita = explode(',', $quantita); 
    $i = 0; 
    foreach($carrelli as $carrello) { 
     $carrello->quantita = $quantita[$i]; 
     $i = $i++; 
    } 
    return Redirect::to('carrello'); 

}); 

在此先感谢。

+0

请确保您将'value'属性传递给'qty []'字段!根据给定的代码,我不认为它! –

+0

当然值有... – rubenSousa

+0

哪部分代码是你得到的错误,它是否在'视图'或其内部的'控制器',如果它的内部任何这些然后在哪一行? –

回答

-1

你为数据库中存在的每个$carreli做迭代,但是你永远不会保存这个值。

Route::post('aggiorna', function() { 
    $quantita = Input::all(); 
    $carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get(); 
    $quantita = explode(',', $quantita); 
    $i = 0; 
    foreach($carrelli as $carrello) { 
     $carrello->quantita = $quantita[$i]; 
     $i = $i++; 
     $carrelo->save(); 
    } 
    return Redirect::to('carrello'); 

}); 

添加$carrelo->save();你为了将它保存在数据库更新后的值。

当您使用Input::all();时还要小心。这意味着您的数据数组包含产品和数量。我会建议使用下面的代码:

Route::post('aggiorna', function() { 
    $quantita = Input::get('qty'); 
    $products = Input::get('products'); 
    $carrelli = \App\Models\Carrello::where('entry_by', \Session::get('uid'))->get(); 
    $quantita = explode(',', $quantita); 
    $products = explode(',', $products); 
    $i = 0; 
    foreach($carrelli as $carrello) { 
     $carrello->quantita = $quantita[$i]; 
     $carrello->products = $products[$i]; 
     $i = $i++; 
     $carrelo->save(); 
    } 
    return Redirect::to('carrello'); 

}); 

然而,因为我不知道你想达到什么样的,我在这里公布这两种解决方案。