将商品更新到购物车(包裹-->darryldecode)Laravel 5.7
Update items to shopping cart(package-->darryldecode) Laravel 5.7
我的blade文件--->
{!! Form::open(['url'=>'/cart/update', 'method'=>'POST']) !!}
<div class="color-quality">
<div class="color-quality-right">
<input type="number" name="qty" value="{{ $cartProduct->quantity }}" min="1">
<input type="hidden" name="id" value="{{ $cartProduct->id }}">
<input type="submit" name="submit" value="update" class="item_add hvr-outline-out button2">
{{-- <button type="submit" name="submit" value="update" class="item_add hvr-outline-out button2">Update</button> --}}
</div>
</div>
{!! Form::close() !!}
路由文件-->
Route::post('/cart/update', 'CartController@updateCart');
控制器文件
public function updateCart(Request $request){
$qty= $request->qty;
$id = $request->id;
/*echo $qty;
echo "<br>";
echo $id;*/
Cart::update($id, $qty);
return redirect('/cart/show');
}
当我回显文件并点击 UPDATE btn 然后我得到这个 id 和数量的结果...... return,另一方面, 得到一些错误...
错误异常 (E_WARNING)
为 foreach()
提供的参数无效
C:\xampp\htdocs\larashop\vendor\darryldecode\cart\src\Darryldecode\Cart\Cart.php
* 更新购物车
*
* @param $id
* @param $数据
*
* $data 将是一个关联数组,你不需要传递所有数据,只需要传递键值
* 您要更新的项目
* @return 布尔值
*/
public 函数更新($id, $data)
{
如果($this->fireEvent('updating',$data)=== false){
return 错误;
}
$cart = $this->getContent();
$item = $cart->pull($id);
foreach ($data as $key => $value) {
// if the key is currently "quantity" we will need to check if an arithmetic
// symbol is present so we can decide if the update of quantity is being added
// or being reduced.
if ($key == 'quantity') {
// we will check if quantity value provided is array,
// if it is, we will need to check if a key "relative" is set
// and we will evaluate its value if true or false,
// this tells us how to treat the quantity value if it should be updated
// relatively to its current quantity value or just totally replace the value
if (is_array($value)) {
if (isset($value['relative'])) {
if ((bool)$value['relative']) {
$item = $this->updateQuantityRelative($item, $key, $value['value']);
} else {
$item = $this->updateQuantityNotRelative($item, $key, $value['value']);
}
}
} else {
$item = $this->updateQuantityRelative($item, $key, $value);
}
参数
"Invalid argument supplied for foreach()"
试试这个:
Cart::update($id, ['quantity' => ['relative' => false,
'value' => $qty
]
]);
我的blade文件--->
{!! Form::open(['url'=>'/cart/update', 'method'=>'POST']) !!}
<div class="color-quality">
<div class="color-quality-right">
<input type="number" name="qty" value="{{ $cartProduct->quantity }}" min="1">
<input type="hidden" name="id" value="{{ $cartProduct->id }}">
<input type="submit" name="submit" value="update" class="item_add hvr-outline-out button2">
{{-- <button type="submit" name="submit" value="update" class="item_add hvr-outline-out button2">Update</button> --}}
</div>
</div>
{!! Form::close() !!}
路由文件-->
Route::post('/cart/update', 'CartController@updateCart');
控制器文件
public function updateCart(Request $request){
$qty= $request->qty;
$id = $request->id;
/*echo $qty;
echo "<br>";
echo $id;*/
Cart::update($id, $qty);
return redirect('/cart/show');
}
当我回显文件并点击 UPDATE btn 然后我得到这个 id 和数量的结果...... return,另一方面, 得到一些错误... 错误异常 (E_WARNING) 为 foreach()
提供的参数无效C:\xampp\htdocs\larashop\vendor\darryldecode\cart\src\Darryldecode\Cart\Cart.php * 更新购物车 * * @param $id * @param $数据 * * $data 将是一个关联数组,你不需要传递所有数据,只需要传递键值 * 您要更新的项目 * @return 布尔值 */ public 函数更新($id, $data) { 如果($this->fireEvent('updating',$data)=== false){ return 错误; }
$cart = $this->getContent();
$item = $cart->pull($id);
foreach ($data as $key => $value) {
// if the key is currently "quantity" we will need to check if an arithmetic
// symbol is present so we can decide if the update of quantity is being added
// or being reduced.
if ($key == 'quantity') {
// we will check if quantity value provided is array,
// if it is, we will need to check if a key "relative" is set
// and we will evaluate its value if true or false,
// this tells us how to treat the quantity value if it should be updated
// relatively to its current quantity value or just totally replace the value
if (is_array($value)) {
if (isset($value['relative'])) {
if ((bool)$value['relative']) {
$item = $this->updateQuantityRelative($item, $key, $value['value']);
} else {
$item = $this->updateQuantityNotRelative($item, $key, $value['value']);
}
}
} else {
$item = $this->updateQuantityRelative($item, $key, $value);
}
参数 "Invalid argument supplied for foreach()"
试试这个:
Cart::update($id, ['quantity' => ['relative' => false,
'value' => $qty
]
]);