【问题标题】:loop is not updating values on table, index wise循环不更新表上的值,索引明智
【发布时间】:2021-06-27 04:26:54
【问题描述】:
$users = DB::table('users')->count();
//dd($users);
for($i=0; $i<$users ; $i++){
   
    $updated=DB::table('users')->where('latehours' , 0)->update(['latehours'=>$total_hours[$i]]);
    $updated=DB::table('users')->where('fine' , 0)->update(['fine'=>$fine[$i]]);
   }

我有一个名为 users 的表,我想分别更新它的两列 latehour 和 fine,两列都在更新,但数组索引的第一个值,但我的循环不起作用。

数组是 $total_hours 和 $fine。 提前致谢

【问题讨论】:

  • 你能分享你的数组值吗?
  • array:3 [▼ 0 => 1 1 => 2 2 => 3 ] 它是 $total_hours 数组
  • array:3 [▼ 0 => 100 1 => 200 2 => 300 ] 它是 $fine 数组
  • 这是你当前代码的预期结果,你想做什么??
  • 我想在用户 'id' 2 和 3 处拥有两个数组的数组索引 1 和索引 2 值

标签: mysql arrays laravel for-loop indexing


【解决方案1】:

您的 where 条件不匹配,因此它不会更新您的记录。

【讨论】:

  • 它正在更新您在我所附图片中看到的值,但它并没有更新索引。表示循环和数组索引不起作用。
【解决方案2】:
   $users = DB::table('users')->count();
   //dd($users);
   for($i=0; $i<$users ; $i++){
   
    $updated=DB::table('users')->where('latehours' , 0)->where('id' , ($i + 1))->update(['latehours'=>$total_hours[$i]]);
    $updatedd=DB::table('users')->where('fine' , 0)->where('id' , ($i + 1))->update(['fine'=>$fine[$i]]);
   // dd($updatedd);
   }

其中存在问题,它没有迭代 id,所以我将 id 的 where 条件放在 where('id' , ($i+1)) 中。现在它运行良好。谢谢大家

db table now

【讨论】:

  • 它在迭代!在第一次迭代之后,每个具有latehours 或fine 0 的用户都更新了,没有用户留下latehours 或fine 0。这看起来好像存在迭代问题。
猜你喜欢
  • 2013-04-29
  • 2016-05-21
  • 2023-02-17
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
相关资源
最近更新 更多