diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index 66cb4a3afe1d..08a860b5f921 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -150,6 +150,19 @@ public function anyFilled($keys) return false; } + /** + * Determine if the request is missing a given input item key. + * + * @param string|array $key + * @return bool + */ + public function missing($key) + { + $keys = is_array($key) ? $key : func_get_args(); + + return ! $this->has($keys); + } + /** * Determine if the given input key is an empty string for "has". * diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index f3656c33b263..d91518592f66 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -300,6 +300,31 @@ public function testHasMethod() $this->assertTrue($request->has('foo.baz')); } + public function testMissingMethod() + { + $request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]); + $this->assertFalse($request->missing('name')); + $this->assertFalse($request->missing('age')); + $this->assertFalse($request->missing('city')); + $this->assertTrue($request->missing('foo')); + $this->assertTrue($request->missing('name', 'email')); + + $request = Request::create('/', 'GET', ['name' => 'Taylor', 'email' => 'foo']); + $this->assertFalse($request->missing('name')); + $this->assertFalse($request->missing('name', 'email')); + + $request = Request::create('/', 'GET', ['foo' => ['bar', 'bar']]); + $this->assertFalse($request->missing('foo')); + + $request = Request::create('/', 'GET', ['foo' => '', 'bar' => null]); + $this->assertFalse($request->missing('foo')); + $this->assertFalse($request->missing('bar')); + + $request = Request::create('/', 'GET', ['foo' => ['bar' => null, 'baz' => '']]); + $this->assertFalse($request->missing('foo.bar')); + $this->assertFalse($request->missing('foo.baz')); + } + public function testHasAnyMethod() { $request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]);