You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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');
}
};
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
: #50030Example:
DB::table('foo')->update(['bar' => collect(['a', 'b'])]);
SQLSTATE[HY093]: Invalid parameter number (Connection: mysql, SQL: update `foo` set `bar` = a)
'["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']]);
update `foo` set `bar` = ["a","b"]
Steps To Reproduce
php artisan tinker
DB::table('foo')->update(['bar' => collect(['a', 'b'])]);
The text was updated successfully, but these errors were encountered: