diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 3c1bf9a81d78..c2036adda8a4 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -3067,13 +3067,31 @@ public function is(Model $model) } /** - * Get all of the current attributes on the model. + * Get a selection of the current attributes on the model. + * + * @param array $keys * * @return array */ - public function getAttributes() + public function getAttributes($keys = []) { - return $this->attributes; + if ($keys === []) { + return $this->attributes; + } + + $keys = is_array($keys) ? $keys : func_get_args(); + + $result = []; + + foreach ($keys as $key) { + $value = $this->getAttribute($key); + + if (! is_null($value)) { + $result[$key] = $value; + } + } + + return $result; } /** diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index 315c31108055..b293cbfb08df 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -31,6 +31,18 @@ public function testAttributeManipulation() $this->assertEquals(json_encode(['name' => 'taylor']), $attributes['list_items']); } + public function testGetMultipleAttributes() + { + $model = new EloquentModelStub(['name' => 'taylor', 'framework' => 'laravel', 'foo' => 'bar']); + + $this->assertEquals(['name' => 'taylor', 'foo' => 'bar'], $model->getAttributes('name', 'foo')); + $this->assertEquals(['name' => 'taylor', 'foo' => 'bar'], $model->getAttributes(['name', 'foo'])); + $this->assertEquals(['name' => 'taylor'], $model->getAttributes('name')); + $this->assertEquals(['name' => 'taylor'], $model->getAttributes(['name'])); + + $this->assertEquals(['name' => 'taylor'], $model->getAttributes(['name', 'doesntexist'])); + } + public function testDirtyAttributes() { $model = new EloquentModelStub(['foo' => '1', 'bar' => 2, 'baz' => 3]);