Skip to content

Commit

Permalink
[9.x] Add setVisible and setHidden to Eloquent Collection (#45558)
Browse files Browse the repository at this point in the history
* Add `setVisible` and `setHidden` to Eloquent Collection

* Update src/Illuminate/Database/Eloquent/Collection.php

Co-authored-by: Nuno Maduro <[email protected]>

* Update src/Illuminate/Database/Eloquent/Collection.php

Co-authored-by: Nuno Maduro <[email protected]>

Co-authored-by: Taylor Otwell <[email protected]>
Co-authored-by: Nuno Maduro <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2023
1 parent 103f0ea commit a656303
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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

0 comments on commit a656303

Please sign in to comment.