简体   繁体   English

使用Laravel更新数据库中的所有记录

[英]Update all records in database using Laravel

I have this: 我有这个:

$keys = Keys_Info::all();

foreach ($keys as $key)
{
          $rank = 1; //Example.. In real it's variable..
          DB::table('keys_info')
                      ->where('id', $key->id)
                      ->update(['rank' => $rank]);
}

This only updates first row, not all. 这只会更新第一行,而不是全部。 How get this to update all rows? 如何获得此更新所有行?

EDIT 编辑

foreach($keys as $key)
{
   print $key->id; //Example printing all keys
   foreach($results as $results)
   {
            print $key->id; //Example printing all keys

            if (in_array($key->example, $array))
            {
               print $key->id; //Example printing first row key!
               $rank = 1; //Example
               DB::table('keywords_info')
                           ->where('id', $key->id)
                           ->update(['url_rank' => $rank]);
            }
   }
}

I didn't explain well at first, this is whole code I use. 起初我没有很好地解释,这是我使用的整个代码。 I figured out that it does not work in IF statement 我发现它在IF语句中不起作用

As you are updating all rows you can simply do like below 在更新所有行时,您可以像下面这样简单地进行操作

 DB::table('keys_info')->update(['rank' => $rank]);

If you still want to pass the id you can do like below 如果您仍然想传递ID,可以按照以下步骤操作

//Fetch all ids in array
$keys=Keys_Info::pluck('id')  OR Keys_Info::value('id')
//apply in query
DB::table('keys_info')->whereIn('id', $keys)->update(['rank' => $rank]);

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

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