Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add setVisible and setHidden to Eloquent Collection #45558

Merged
merged 3 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,28 @@ public function makeVisible($attributes)
return $this->each->makeVisible($attributes);
}

/**
* Set the visible attributes across the entire collection.
*
* @param array<int, string> $visible
* @return $this
*/
public function setVisible($visible)
{
return $this->each->setVisible($visible);
}

/**
* Set the hidden attributes across the entire collection.
*
* @param array<int, string> $hidden
* @return $this
*/
public function setHidden($hidden)
{
return $this->each->setHidden($hidden);
}

/**
* Append an attribute across the entire collection.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Database/DatabaseEloquentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,22 @@ public function testMakeVisibleRemovesHiddenFromEntireCollection()
$this->assertEquals([], $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 testAppendsAddsTestOnEntireCollection()
{
$c = new Collection([new TestEloquentCollectionModel]);
Expand Down