Skip to content

Commit a423636

Browse files
Add mergeHidden and mergeVisible methods to Collection class with tests (#58110)
1 parent 9807448 commit a423636

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Illuminate/Database/Eloquent/Collection.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,28 @@ public function makeVisible($attributes)
580580
return $this->each->makeVisible($attributes);
581581
}
582582

583+
/**
584+
* Merge the given, typically visible, attributes hidden across the entire collection.
585+
*
586+
* @param array<array-key, string>|string $attributes
587+
* @return $this
588+
*/
589+
public function mergeHidden($attributes)
590+
{
591+
return $this->each->mergeHidden($attributes);
592+
}
593+
594+
/**
595+
* Merge the given, typically hidden, attributes visible across the entire collection.
596+
*
597+
* @param array<array-key, string>|string $attributes
598+
* @return $this
599+
*/
600+
public function mergeVisible($attributes)
601+
{
602+
return $this->each->mergeVisible($attributes);
603+
}
604+
583605
/**
584606
* Set the visible attributes across the entire collection.
585607
*

tests/Database/DatabaseEloquentCollectionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,22 @@ public function testMakeVisibleRemovesHiddenFromEntireCollection()
545545
$this->assertEquals([], $c[0]->getHidden());
546546
}
547547

548+
public function testMergeHiddenAddsHiddenOnEntireCollection()
549+
{
550+
$c = new Collection([new TestEloquentCollectionModel]);
551+
$c = $c->mergeHidden(['merged']);
552+
553+
$this->assertEquals(['hidden', 'merged'], $c[0]->getHidden());
554+
}
555+
556+
public function testMergeVisibleRemovesHiddenFromEntireCollection()
557+
{
558+
$c = new Collection([new TestEloquentCollectionModel]);
559+
$c = $c->mergeVisible(['merged']);
560+
561+
$this->assertEquals(['visible', 'merged'], $c[0]->getVisible());
562+
}
563+
548564
public function testSetVisibleReplacesVisibleOnEntireCollection()
549565
{
550566
$c = new Collection([new TestEloquentCollectionModel]);

0 commit comments

Comments
 (0)