2016-03-15 80 views
1

大家好,我的数据库结构如下:批量更新查询没有口才Foreach循环 - Laravel 5.1

表:分析

id | user_id(fk) | date | finalized(boolean) 

现在我想更新大量数据。例如,我想选择user_id = 1和日期BETWEEN 2016-03-01和2016-03-20的数据。然后我想更新从0到1的所有那些记录,我得到的结果列。

我做了什么如下:

Analytics::where('user_id', $request->get('user_id')) 
->whereBetween('date', [$request->get('from_date'), $request->get('to_date')]) 
->update(array('finalized' => 1)); 

但这不工作,没有表现出任何的错误。请让我知道如何在雄辩的帮助下做些什么。提前致谢。

我也看到了Stackover Flow中的其他问题,但答案建议运行foreach循环,这是我不想要的。那么有没有什么办法,然后好心劝我。再次感谢

+0

的可能的复制[雄辩模型大规模更新](http://stackoverflow.com/questions/22430716/eloquent-model-mass-update) – cwang

+0

@crystalwill,我已经看到了这个问题太,但没有找到我真正想要的东西。 – Siddharth

回答

2

您确定属性设置在Analytics模型上,它包括finalized?另外,您是否确认请求对象具有您期望它具有的日期和用户ID?

另外,你可以去没有雄辩和直接击中数据库。

DB::table('analytics') 
    ->where('user_id', $request->user_id)-> 
    ->whereBetween('date', [$request->from_date, $request->to_date]) 
    ->update(['finalized' => 1]); 
+0

我正在检查。感谢您的建议 – Siddharth