Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
omergy opened this issue Oct 18, 2024 · 1 comment · Fixed by #53254
Closed

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

omergy opened this issue Oct 18, 2024 · 1 comment · Fixed by #53254

Comments

@omergy
Copy link

omergy commented Oct 18, 2024

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'])]);
@omergy
Copy link
Author

omergy commented Oct 22, 2024

Thanks for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants