1
votes

I am trying to update all rows inside one of my database tables but I am receiving the following error:

Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically

I am doing this inside a migration:

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();
    });
}

I am adding a json column 'beds' to my existing table 'rooms' and want to set a default value of ['One King', 'Two Doubles'], I am wondering if I can do it directly in the query without loading the models.

I made up the following solution, but it feels kinda "hacky":

// All id's are bigger than 0
Room::where('id', '>', 0)->update([
    'beds' => ['One King', 'Two Doubles']
]);

Does anyone know of another way to do update all rows without the where statement?

1
depending on configuration of the server without the where clause you get an errornbk

1 Answers

3
votes

You can call the static query() method on the model:

// ::query() return an instance of \Illuminate\Database\Eloquent\Builder
Room::query()->update([
  'beds' => ['One King', 'Two Doubles']
]);