如何通过数组的每个元素一个一个地更新表的列?

Posted

技术标签:

【中文标题】如何通过数组的每个元素一个一个地更新表的列?【英文标题】:how to update column of table by each element of array one by one? 【发布时间】:2021-06-26 10:39:59 【问题描述】:

我想更新表格的列

$update = DB::table('users')->where('latehours' , '');

foreach($update as $up) 
    $up->update([
        'latehours' => $total_hours
    ]);

【问题讨论】:

\Illuminate\Database\Eloquent\Builder 不可迭代。你的意思是DB::table('users')->where('latehours' , '')->get() 【参考方案1】:

您可以直接更新您的表,而无需从数据库中获取数据:

$updated = DB::table('users')->where('latehours', '')
                             ->update(['latehours' => $total_hours]);

【讨论】:

以上是关于如何通过数组的每个元素一个一个地更新表的列?的主要内容,如果未能解决你的问题,请参考以下文章