diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 991f06434a5b..658b84dd8c1c 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -1889,6 +1889,19 @@ public function only($attributes) return $results; } + /** + * Get a subset of the model's attributes that aren't in the given array. + * + * @param array|string $attributes + * @return array + */ + public function except($attributes) + { + $attributes = is_array($attributes) ? $attributes : func_get_args(); + + return Arr::except($this->getAttributes(), $attributes); + } + /** * Sync the original attributes with the current. * diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index 1966e5550bee..e208f5433cd7 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -486,6 +486,18 @@ public function testOnly() $this->assertEquals(['first_name' => 'taylor', 'last_name' => 'otwell'], $model->only(['first_name', 'last_name'])); } + public function testExcept() + { + $model = new EloquentModelStub; + $model->first_name = 'Taylor'; + $model->last_name = 'Otwell'; + $model->project = 'laravel'; + + $this->assertEquals(['first_name' => 'Taylor', 'last_name' => 'Otwell'], $model->except('project')); + $this->assertEquals(['project' => 'laravel'], $model->except('first_name', 'last_name')); + $this->assertEquals([], $model->except(['first_name', 'last_name', 'project'])); + } + public function testNewInstanceReturnsNewInstanceWithAttributesSet() { $model = new EloquentModelStub;