Skip to content

Commit

Permalink
Restores the "addVisible" / "addHidden" methods removed in Laravel 7
Browse files Browse the repository at this point in the history
It is recommended to use "makeVisible" / "makeHidden" in new code going forward.

Fixes wintercms/winter#567
  • Loading branch information
bennothommo committed Jun 24, 2022
1 parent 9e2eca0 commit 1643791
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Database/Concerns/HidesAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Winter\Storm\Database\Concerns;

/**
* Hides and shows attributes for serialization.
*/
trait HidesAttributes
{
/**
* Add hidden attributes for the model.
*
* This restores the `addHidden` method that was removed from Laravel 7 onwards. It is however recommended to use
* the `makeHidden` method going forward.
*
* @param array|string|null $attributes
*/
public function addHidden($attributes = null): void
{
$this->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()
);
}
}
1 change: 1 addition & 0 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
68 changes: 68 additions & 0 deletions tests/Database/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,46 @@ public function testMassAssignmentOnFieldsNotInDatabase()
$this->assertNull($model->name);
}

public function testVisibleAttributes()

This comment has been minimized.

Copy link
@bennothommo

bennothommo Jun 24, 2022

Author Member

Just as a bit of trivia - this test as well as the one below was 90% written by GitHub Copilot. Actually did very well with creating a unit test case from scratch :)

{
$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();
});
Expand All @@ -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';
}

0 comments on commit 1643791

Please sign in to comment.