如何在+1周周期内批量更新多个Comment
记录的发布日期?所需结果的说明:
$interval = CarbonInterval::week(); // 1 week interval, that needs to fit into below query
Comment::where('id', '>', 10)->update('publish_date', ...);
您可以使用DB::raw()
在数据库中执行任意代码,但请记住,它可能无法移植到其他数据库引擎。
在MySQL和其他支持DATE_ADD函数的引擎中,以下操作应该可以完成:
Comment::where('id', '>', 10)->update(['publish_date' => DB::raw('DATE_ADD("publish_date", INTERVAL 1 WEEK)')]);
根据文档,您可以执行以下操作:
App'Models'Comment::where('id', '>', 10)->update([
'publish_date' => Carbon'Carbon::parse(DB::raw("`publish_date`"))->addWeeks(1)
]);
据此:
https://laracasts.com/discuss/channels/eloquent/mass-updating-table-to-set-value-of-column-to-value-of-another-column
基本更新的Laravel文档:
https://laravel.com/docs/5.2/eloquent#basic-更新
加减碳文档:
http://carbon.nesbot.com/docs/#api-addsub