Skip to content

Commit 2044605

Browse files
[11.x] Fix incorrect bindings in DB::update when using a collection as a value (#53254)
* [11.x] Fix incorrect bindings in DB::update when using a collection as a value fixes #53226 Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * Update Builder.php --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent f776438 commit 2044605

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3848,7 +3848,11 @@ public function update(array $values)
38483848

38493849
$values = collect($values)->map(function ($value) {
38503850
if (! $value instanceof Builder) {
3851-
return ['value' => $value, 'bindings' => $value];
3851+
return ['value' => $value, 'bindings' => match (true) {
3852+
$value instanceof Collection => $value->all(),
3853+
$value instanceof UnitEnum => enum_value($value),
3854+
default => $value,
3855+
}];
38523856
}
38533857

38543858
[$query, $bindings] = $this->parseSub($value);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Database;
4+
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Facades\Schema;
8+
use Orchestra\Testbench\Attributes\RequiresDatabase;
9+
use PHPUnit\Framework\Attributes\DataProvider;
10+
11+
require_once 'Enums.php';
12+
13+
class QueryBuilderUpdateTest extends DatabaseTestCase
14+
{
15+
protected function afterRefreshingDatabase()
16+
{
17+
Schema::create('example', function (Blueprint $table) {
18+
$table->increments('id');
19+
$table->string('name')->nullable();
20+
$table->string('title')->nullable();
21+
$table->string('status')->nullable();
22+
$table->json('payload')->nullable();
23+
});
24+
}
25+
26+
#[DataProvider('jsonValuesDataProvider')]
27+
#[RequiresDatabase(['sqlite', 'mysql', 'mariadb'])]
28+
public function testBasicUpdateForJson($column, $given, $expected)
29+
{
30+
DB::table('example')->insert([
31+
'name' => 'Taylor Otwell',
32+
'title' => 'Mr.',
33+
]);
34+
35+
DB::table('example')->update([$column => $given]);
36+
37+
$this->assertDatabaseHas('example', [
38+
'name' => 'Taylor Otwell',
39+
'title' => 'Mr.',
40+
$column => $column === 'payload' ? $this->castAsJson($expected) : $expected,
41+
]);
42+
}
43+
44+
public static function jsonValuesDataProvider()
45+
{
46+
yield ['payload', ['Laravel', 'Founder'], ['Laravel', 'Founder']];
47+
yield ['payload', collect(['Laravel', 'Founder']), ['Laravel', 'Founder']];
48+
yield ['status', StringStatus::draft, 'draft'];
49+
}
50+
}

0 commit comments

Comments
 (0)