Skip to content

Incorrect bindings in DB::update when using a collection as a value #53226

@omergy

Description

@omergy

Laravel Version

11.26.0

PHP Version

8.3.12

Database Driver & Version

MySQL

Description

When using a collection as a column value in DB::update, the wrong bindings are applied.
Before Laravel 11, the collection would be turned into a string (which still is the case if you use an array) but now all the values of the collection is spread and added to the bindings array.

The change seems to have come from this pull request with the added Arr::flatten: #50030

Example:

  • DB::table('foo')->update(['bar' => collect(['a', 'b'])]);
  • gives the error SQLSTATE[HY093]: Invalid parameter number (Connection: mysql, SQL: update `foo` set `bar` = a)
  • Expected is that the collection should become a (json)string '["a","b"]' and be saved in the database.

With an array it still works (and this is how it was in version < 11 with collections)

  • DB::table('foo')->update(['bar' => ['a', 'b']]);
  • correctly updates with: update `foo` set `bar` = ["a","b"]

Steps To Reproduce

  • Use Laravel 11.x
  • Create (example) table:
 use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('foo', function (Blueprint $table) { $table->id(); $table->string('bar'); }); } public function down(): void { Schema::dropIfExists('foo'); } }; 
  • php artisan tinker
  • DB::table('foo')->update(['bar' => collect(['a', 'b'])]);

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