diff --git a/src/Illuminate/Support/Traits/InteractsWithData.php b/src/Illuminate/Support/Traits/InteractsWithData.php index bef9de2e5ccc..0855a8ea45e2 100644 --- a/src/Illuminate/Support/Traits/InteractsWithData.php +++ b/src/Illuminate/Support/Traits/InteractsWithData.php @@ -264,6 +264,22 @@ public function string($key, $default = null) return new Stringable($this->data($key, $default)); } + /** + * Retrieve data from the instance as a string. + * + * @param string $key + * @param mixed $default + * @return string|null + */ + public function plainString($key, $default = null) + { + if ($this->isNotFilled($key)) { + return value($default); + } + + return (string) $this->data($key); + } + /** * Retrieve data as a boolean value. * diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index 078bc04612fc..885e732814c2 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -683,6 +683,31 @@ public function testStringMethod() $this->assertSame('', $request->string('unknown_key')->value()); } + public function testPlainStringMethod() + { + $request = Request::create('/', 'GET', [ + 'int' => 123, + 'int_str' => '456', + 'float' => 123.456, + 'float_str' => '123.456', + 'float_zero' => 0.000, + 'float_str_zero' => '0.000', + 'str' => 'abc', + 'empty_str' => '', + 'null' => null, + ]); + $this->assertSame('123', $request->plainString('int')); + $this->assertSame('456', $request->plainString('int_str')); + $this->assertSame('123.456', $request->plainString('float')); + $this->assertSame('123.456', $request->plainString('float_str')); + $this->assertSame('0', $request->plainString('float_zero')); + $this->assertSame('0.000', $request->plainString('float_str_zero')); + $this->assertSame(null, $request->plainString('empty_str')); + $this->assertSame(null, $request->plainString('null')); + $this->assertSame(null, $request->plainString('unknown_key')); + $this->assertSame('default', $request->plainString('unknown_key', 'default')); + } + public function testBooleanMethod() { $request = Request::create('/', 'GET', ['with_trashed' => 'false', 'download' => true, 'checked' => 1, 'unchecked' => '0', 'with_on' => 'on', 'with_yes' => 'yes']);