From 1643791cc13fac0b9724e6cd095f12387f7ddfb2 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Fri, 24 Jun 2022 15:00:41 +0800 Subject: [PATCH] Restores the "addVisible" / "addHidden" methods removed in Laravel 7 It is recommended to use "makeVisible" / "makeHidden" in new code going forward. Fixes https://github.com/wintercms/winter/issues/567 --- src/Database/Concerns/HidesAttributes.php | 41 ++++++++++++++ src/Database/Model.php | 1 + tests/Database/ModelTest.php | 68 +++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 src/Database/Concerns/HidesAttributes.php diff --git a/src/Database/Concerns/HidesAttributes.php b/src/Database/Concerns/HidesAttributes.php new file mode 100644 index 000000000..2f38c27aa --- /dev/null +++ b/src/Database/Concerns/HidesAttributes.php @@ -0,0 +1,41 @@ +hidden = array_merge( + $this->hidden, + is_array($attributes) ? $attributes : func_get_args() + ); + } + + /** + * Add visible attributes for the model. + * + * This restores the `addVisible` method that was removed from Laravel 7 onwards. It is however recommended to use + * the `makeVisible` method going forward. + * + * @param array|string|null $attributes + */ + public function addVisible($attributes = null): void + { + $this->visible = array_merge( + $this->visible, + is_array($attributes) ? $attributes : func_get_args() + ); + } +} diff --git a/src/Database/Model.php b/src/Database/Model.php index 9f2666a21..4e5315fad 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -20,6 +20,7 @@ class Model extends EloquentModel { use Concerns\GuardsAttributes; use Concerns\HasRelationships; + use Concerns\HidesAttributes; use \Winter\Storm\Support\Traits\Emitter; use \Winter\Storm\Extension\ExtendableTrait; use \Winter\Storm\Database\Traits\DeferredBinding; diff --git a/tests/Database/ModelTest.php b/tests/Database/ModelTest.php index d5fb8ffa4..1f72229c1 100644 --- a/tests/Database/ModelTest.php +++ b/tests/Database/ModelTest.php @@ -78,12 +78,46 @@ public function testMassAssignmentOnFieldsNotInDatabase() $this->assertNull($model->name); } + public function testVisibleAttributes() + { + $model = TestModelVisible::create([ + 'name' => 'Visible Test', + 'data' => 'Test data', + 'description' => 'Test description', + 'meta' => 'Some meta data' + ]); + + $this->assertArrayNotHasKey('meta', $model->toArray()); + + $model->addVisible('meta'); + + $this->assertArrayHasKey('meta', $model->toArray()); + } + + public function testHiddenAttributes() + { + $model = TestModelHidden::create([ + 'name' => 'Hidden Test', + 'data' => 'Test data', + 'description' => 'Test description', + 'meta' => 'Some meta data' + ]); + + $this->assertArrayHasKey('description', $model->toArray()); + + $model->addHidden('description'); + + $this->assertArrayNotHasKey('description', $model->toArray()); + } + protected function createTable() { $this->getBuilder()->create('test_model', function ($table) { $table->increments('id'); $table->string('name')->nullable(); $table->text('data')->nullable(); + $table->text('description')->nullable(); + $table->text('meta')->nullable(); $table->boolean('on_guard')->nullable(); $table->timestamps(); }); @@ -109,3 +143,37 @@ public function beforeSave() } } } + +class TestModelVisible extends Model +{ + public $fillable = [ + 'name', + 'data', + 'description', + 'meta' + ]; + + public $visible = [ + 'id', + 'name', + 'description' + ]; + + public $table = 'test_model'; +} + +class TestModelHidden extends Model +{ + public $fillable = [ + 'name', + 'data', + 'description', + 'meta' + ]; + + public $hidden = [ + 'meta', + ]; + + public $table = 'test_model'; +}