【发布时间】:2020-07-30 08:13:08
【问题描述】:
我正在尝试更新我的一个数据库表中的所有行,但收到以下错误:
不应静态调用非静态方法 Illuminate\Database\Eloquent\Model::update()
我在迁移中这样做:
public function up()
{
Schema::table('rooms', function (Blueprint $table) {
$table->json('beds')->nullable();
});
Room::update([
'beds' => ['One King', 'Two Doubles']
]);
Schema::table('rooms', function (Blueprint $table) {
$table->json('beds')->nullable(false)->change();
});
}
我正在向现有表“房间”添加一个 json 列“床”,并想设置默认值 ['One King', 'Two Doubles'],我想知道是否可以直接在查询中执行此操作而不加载模型。
我提出了以下解决方案,但感觉有点“hacky”:
// All id's are bigger than 0
Room::where('id', '>', 0)->update([
'beds' => ['One King', 'Two Doubles']
]);
有没有人知道在没有 where 语句的情况下更新所有行的另一种方法?
【问题讨论】:
-
取决于服务器的配置,没有 where 子句你会得到一个错误
标签: mysql laravel eloquent laravel-6