Skip to content

Commit bda4494

Browse files
[12.x] Add default parameter support to Request::fluent() method (#57840)
* Fix issue * Add test * Formatting
1 parent c17a10b commit bda4494

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/Illuminate/Http/Concerns/InteractsWithInput.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,14 @@ public function input($key = null, $default = null)
118118
* Retrieve input from the request as a Fluent object instance.
119119
*
120120
* @param array|string|null $key
121+
* @param array $default
121122
* @return \Illuminate\Support\Fluent
122123
*/
123-
public function fluent($key = null)
124+
public function fluent($key = null, array $default = [])
124125
{
125-
return new Fluent(is_array($key) ? $this->only($key) : $this->input($key));
126+
$value = is_array($key) ? $this->only($key) : $this->input($key);
127+
128+
return new Fluent($value ?? $default);
126129
}
127130

128131
/**

tests/Http/HttpRequestTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,20 @@ public function testInputMethod()
591591
$this->assertInstanceOf(SymfonyUploadedFile::class, $request['file']);
592592
}
593593

594+
public function testFluentMethod()
595+
{
596+
$request = Request::create('/', 'GET', [
597+
'user' => [
598+
'name' => 'Michael',
599+
'role' => 'admin',
600+
],
601+
'users' => null,
602+
]);
603+
$this->assertSame(['name' => 'Michael', 'role' => 'admin'], $request->fluent('user')->toArray());
604+
$this->assertSame([], $request->fluent('users')->toArray());
605+
$this->assertSame([], $request->fluent('not_found')->toArray());
606+
}
607+
594608
public function testStringMethod()
595609
{
596610
$request = Request::create('/', 'GET', [

0 commit comments

Comments
 (0)