Added setVisible and setHidden into Model\Collection. (#7147)

This commit is contained in:
WenRenHai 2024-11-11 13:55:47 +08:00 committed by GitHub
parent 70fc945dec
commit 2788a2f069
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 0 deletions

View File

@ -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

View File

@ -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.
*

View File

@ -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()]);