Skip to content

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

@SjorsO

Description

@SjorsO

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 #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions