Skip to content

Commit 42be005

Browse files
[12.x] mergeIfMissing allows merging with nested arrays (#52242)
* feat: mergeIfMissing allows merging with nested arrays * Fix failing tests * Fix styling
1 parent 6aae2a3 commit 42be005

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/Illuminate/Http/Request.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,13 @@ public function userAgent()
349349
*/
350350
public function merge(array $input)
351351
{
352-
$this->getInputSource()->add($input);
353-
354-
return $this;
352+
return tap($this, function (Request $request) use ($input) {
353+
$request->getInputSource()
354+
->replace(collect($input)->reduce(
355+
fn ($requestInput, $value, $key) => data_set($requestInput, $key, $value),
356+
$this->getInputSource()->all()
357+
));
358+
});
355359
}
356360

357361
/**
@@ -547,8 +551,8 @@ public function hasSession(bool $skipIfUninitialized = false): bool
547551
public function getSession(): SessionInterface
548552
{
549553
return $this->hasSession()
550-
? $this->session
551-
: throw new SessionNotFoundException;
554+
? $this->session
555+
: throw new SessionNotFoundException;
552556
}
553557

554558
/**

tests/Http/HttpRequestTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,16 @@ public function testMergeIfMissingMethod()
983983
$request->mergeIfMissing($merge);
984984
$this->assertSame('Taylor', $request->input('name'));
985985
$this->assertSame(1, $request->input('boolean_setting'));
986+
987+
$request = Request::create('/', 'GET', ['user' => ['first_name' => 'Taylor', 'email' => 'taylor@laravel.com']]);
988+
$merge = ['user.last_name' => 'Otwell'];
989+
$request->mergeIfMissing($merge);
990+
$this->assertSame('Otwell', $request->input('user.last_name'));
991+
992+
$request = Request::create('/', 'GET', ['user' => ['first_name' => 'Taylor', 'email' => 'taylor@laravel.com']]);
993+
$merge = ['user.first_name' => 'John'];
994+
$request->mergeIfMissing($merge);
995+
$this->assertSame('Taylor', $request->input('user.first_name'));
986996
}
987997

988998
public function testReplaceMethod()

0 commit comments

Comments
 (0)