Skip to content

[11.x] Changes in #53948 cause upsert to fail #54145

@kohenkatz

Description

@kohenkatz

Laravel Version

11.37.0

PHP Version

8.3.13

Database Driver & Version

PostgreSQL 17

Description

When providing an array with non-numeric keys to the Model::upsert method, the behavior in 11.36.0 and older was to use the values of the array as the data. After the changes in #53948, the keys of the array are used as the data, and a database error is returned (because there are no such column names, or because the number of keys does not match the number of values provided).

I understand the rationale for #53948, and I think it makes sense to keep that behavior for Model::insert, but the same behavior makes no sense for Model::upsert because the documentation explicitly describes upsert's first argument as an array of records, not a single record, so using the values makes more sense.

Steps To Reproduce

The following code works in 11.36.0, but crashes with a database error in 11.37.0:

(This is using Laravel's default users table to make it as simple as possible and not need to give a migration as part of the example.)

$users = [
    'user-a' => ['name' => 'User A', 'email': 'user-a@example.com', 'password' => Hash::make('qwerty')],
    'user-b' => ['name' => 'User B', 'email': 'user-b@example.net', 'password' => Hash::make('asdfgh')],
];

User::upsert($users, ['email'], ['name', 'password']);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions