framework icon indicating copy to clipboard operation
framework copied to clipboard

`$model->incrementEach()` affects all models in the database table (instead of just `$model`)

Open SjorsO opened this issue 2 months ago • 3 comments

Laravel Version

12.28.1

PHP Version

8.4

Database Driver & Version

No response

Description

Bad way to end the week: I just found out a bunch of our production data is corrupt because of this:

// affects just the `$model` database record
$model->increment('number');

// affects all records in the database table 😢 
$model->incrementEach([
    'number' => 1,
    'another_number' => 1,
]); 

This has been reported before in https://github.com/laravel/framework/issues/49009, but that issue was closed.

It's easy to assume that $model->increment() behaves the same way as $model->incrementEach(). Unfortunately, that assumption leads to corrupted production data. Maybe we can keep this issue open this time to try and get this fixed?

Steps To Reproduce

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->unsignedInteger('number')->default(0);
    $table->timestamps();
});


$post1 = Post::create(['number' => 0]);
$post2 = Post::create(['number' => 0]);

$this->assertSame(0, $post1->refresh()->number);
$this->assertSame(0, $post2->refresh()->number);

$post1->incrementEach(['number' => 1]);

$this->assertSame(1, $post1->refresh()->number);
$this->assertSame(0, $post2->refresh()->number); // this fails

SjorsO avatar Oct 03 '25 12:10 SjorsO