Skip to content

Commit 5073f78

Browse files
authored
Add new mergeVisible, mergeHidden and mergeAppends methods. (#56678)
* Add new mergeVisible, mergeHidden and mergeAppends methods. * Spelling error * Style fixes
1 parent 7796b9b commit 5073f78

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,19 @@ public function setAppends(array $appends)
23852385
return $this;
23862386
}
23872387

2388+
/**
2389+
* Merge new appended attributes with existing appended attributes on the model.
2390+
*
2391+
* @param array<string> $appends
2392+
* @return $this
2393+
*/
2394+
public function mergeAppends(array $appends)
2395+
{
2396+
$this->appends = array_values(array_unique(array_merge($this->appends, $appends)));
2397+
2398+
return $this;
2399+
}
2400+
23882401
/**
23892402
* Return whether the accessor attribute has been appended.
23902403
*

src/Illuminate/Database/Eloquent/Concerns/HidesAttributes.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ public function setHidden(array $hidden)
4141
return $this;
4242
}
4343

44+
/**
45+
* Merge new hidden attributes with existing hidden attributes on the model.
46+
*
47+
* @param array<string> $hidden
48+
* @return $this
49+
*/
50+
public function mergeHidden(array $hidden)
51+
{
52+
$this->hidden = array_values(array_unique(array_merge($this->hidden, $hidden)));
53+
54+
return $this;
55+
}
56+
4457
/**
4558
* Get the visible attributes for the model.
4659
*
@@ -64,6 +77,19 @@ public function setVisible(array $visible)
6477
return $this;
6578
}
6679

80+
/**
81+
* Merge new visible attributes with existing visible attributes on the model.
82+
*
83+
* @param array<string> $visible
84+
* @return $this
85+
*/
86+
public function mergeVisible(array $visible)
87+
{
88+
$this->visible = array_values(array_unique(array_merge($this->visible, $visible)));
89+
90+
return $this;
91+
}
92+
6793
/**
6894
* Make the given, typically hidden, attributes visible.
6995
*

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,18 @@ public function testHidden()
14951495
$this->assertArrayNotHasKey('age', $array);
14961496
}
14971497

1498+
public function testMergeHiddenMergesHidden()
1499+
{
1500+
$model = new EloquentModelHiddenStub;
1501+
1502+
$hiddenCount = count($model->getHidden());
1503+
$this->assertContains('foo', $model->getHidden());
1504+
1505+
$model->mergeHidden(['bar']);
1506+
$this->assertCount($hiddenCount + 1, $model->getHidden());
1507+
$this->assertContains('bar', $model->getHidden());
1508+
}
1509+
14981510
public function testVisible()
14991511
{
15001512
$model = new EloquentModelStub(['name' => 'foo', 'age' => 'bar', 'id' => 'baz']);
@@ -1504,6 +1516,18 @@ public function testVisible()
15041516
$this->assertArrayNotHasKey('age', $array);
15051517
}
15061518

1519+
public function testMergeVisibleMergesVisible()
1520+
{
1521+
$model = new EloquentModelVisibleStub;
1522+
1523+
$visibleCount = count($model->getVisible());
1524+
$this->assertContains('foo', $model->getVisible());
1525+
1526+
$model->mergeVisible(['bar']);
1527+
$this->assertCount($visibleCount + 1, $model->getVisible());
1528+
$this->assertContains('bar', $model->getVisible());
1529+
}
1530+
15071531
public function testDynamicHidden()
15081532
{
15091533
$model = new EloquentModelDynamicHiddenStub(['name' => 'foo', 'age' => 'bar', 'id' => 'baz']);
@@ -2421,6 +2445,18 @@ public function testAppendingOfAttributes()
24212445
$this->assertEquals([], $model->toArray());
24222446
}
24232447

2448+
public function testMergeAppendsMergesAppends()
2449+
{
2450+
$model = new EloquentModelAppendsStub;
2451+
2452+
$appendsCount = count($model->getAppends());
2453+
$this->assertEquals(['is_admin', 'camelCased', 'StudlyCased'], $model->getAppends());
2454+
2455+
$model->mergeAppends(['bar']);
2456+
$this->assertCount($appendsCount + 1, $model->getAppends());
2457+
$this->assertContains('bar', $model->getAppends());
2458+
}
2459+
24242460
public function testGetMutatedAttributes()
24252461
{
24262462
$model = new EloquentModelGetMutatorsStub;
@@ -3905,6 +3941,18 @@ public function getHidden()
39053941
}
39063942
}
39073943

3944+
class EloquentModelVisibleStub extends Model
3945+
{
3946+
protected $table = 'stub';
3947+
protected $visible = ['foo'];
3948+
}
3949+
3950+
class EloquentModelHiddenStub extends Model
3951+
{
3952+
protected $table = 'stub';
3953+
protected $hidden = ['foo'];
3954+
}
3955+
39083956
class EloquentModelDynamicVisibleStub extends Model
39093957
{
39103958
protected $table = 'stub';

0 commit comments

Comments
 (0)