【问题标题】:Non-numeric value passed to decrement method传递给递减方法的非数字值
【发布时间】:2018-05-09 04:11:57
【问题描述】:
foreach($menuItem1 as &$a){
  $a = $a*$amount;
}

$numArray = array_map('intval', $menuItem1);

foreach($menuItem2 as $in){
    DB::table('ingredients')->where('id', $in)->decrement('ingredient_stock', $numArray);
}

$numArray 的结果:
数组(3) { [0]=> int(6) [1]=> int(8) [2]=> int(24) } 数组(2) { [0]=> int(1) [1] => int(2) }

我想运行一个查询,但是得到这个错误:Non-numeric value passing to decrement method。为什么?

【问题讨论】:

  • $numArray 是数组,好吗?这真的不是一个数值。
  • 好的,你有解决办法吗?
  • 试试 dd($numArray)??
  • 没有更多错误,但是现在没有执行查询。 DB::table('ingredients')->where('id', $in)->decrement('ingredient_stock', dd($numArray));
  • 为什么“不执行”?错误?

标签: php arrays laravel foreach


【解决方案1】:

decrement 方法中传递您想要减少多少的值。

试试下面的。

$num_i = 0;
foreach($menuItem2 as $in){
    DB::table('ingredients')->where('id', $in)->decrement('ingredient_stock', $numArray[$num_i]);
    $num_i = $num_i + 1;
}

【讨论】:

  • 乐于助人!!
【解决方案2】:

decrement 需要一个数值、一个数字,而不是一个数字数组。

你需要重组你的$numArray。你可以把它做成一个关联数组,以菜单项 id 作为键 [ $menuItem1ID => valueForMenuItem1, $menuItem2ID => valueForMenuItem2, ]

然后在你的循环中而不是->decrement('ingredient_stock', $numArray);->decrement('ingredient_stock', $numArray[$in]);

【讨论】:

    猜你喜欢
    • 2011-12-07
    • 1970-01-01
    • 2017-03-11
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2011-12-04
    • 1970-01-01
    相关资源
    最近更新 更多