From 2788a2f06922146129327f0c204db7da1f9e8a95 Mon Sep 17 00:00:00 2001 From: WenRenHai Date: Mon, 11 Nov 2024 13:55:47 +0800 Subject: [PATCH] Added `setVisible` and `setHidden` into `Model\Collection`. (#7147) --- CHANGELOG-3.1.md | 1 + src/database/src/Model/Collection.php | 16 ++++++++++++++++ src/database/tests/ModelCollectionTest.php | 16 ++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index d06d302e1..90b5624f9 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -4,6 +4,7 @@ - [#7141](https://github.com/hyperf/hyperf/pull/7141) Added method `Hyperf\Collection\Arr::shuffleAssoc`. - [#7143](https://github.com/hyperf/hyperf/pull/7143) Added method `Hyperf\Database\Model\Builder::findOr`. +- [#7147](https://github.com/hyperf/hyperf/pull/7147) Added `setVisible` and `setHidden` into `Model\Collection`. ## Fixed diff --git a/src/database/src/Model/Collection.php b/src/database/src/Model/Collection.php index bdef77870..5332cdf71 100755 --- a/src/database/src/Model/Collection.php +++ b/src/database/src/Model/Collection.php @@ -521,6 +521,22 @@ class Collection extends BaseCollection implements CompressInterface return $this->each->makeVisible($attributes); } + /** + * Set the visible attributes across the entire collection. + */ + public function setVisible(array $visible): static + { + return $this->each->setVisible($visible); + } + + /** + * Set the hidden attributes across the entire collection. + */ + public function setHidden(array $hidden): static + { + return $this->each->setHidden($hidden); + } + /** * Append an attribute across the entire collection. * diff --git a/src/database/tests/ModelCollectionTest.php b/src/database/tests/ModelCollectionTest.php index 4d16e7f22..9ba49bd0a 100644 --- a/src/database/tests/ModelCollectionTest.php +++ b/src/database/tests/ModelCollectionTest.php @@ -456,6 +456,22 @@ class ModelCollectionTest extends TestCase $this->assertEquals(['hidden', 'visible'], $c[0]->getHidden()); } + public function testSetVisibleReplacesVisibleOnEntireCollection() + { + $c = new Collection([new TestEloquentCollectionModel()]); + $c = $c->setVisible(['hidden']); + + $this->assertEquals(['hidden'], $c[0]->getVisible()); + } + + public function testSetHiddenReplacesHiddenOnEntireCollection() + { + $c = new Collection([new TestEloquentCollectionModel()]); + $c = $c->setHidden(['visible']); + + $this->assertEquals(['visible'], $c[0]->getHidden()); + } + public function testMakeVisibleRemovesHiddenFromEntireCollection() { $c = new Collection([new TestEloquentCollectionModel()]);