diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index 59f3a3f52908..0c3bad61232a 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -398,6 +398,16 @@ public function makeVisible($attributes) return $this->each->makeVisible($attributes); } + /** + * Append an attribute across the entire collection. + * @param $attributes + * @return mixed + */ + public function append($attributes) + { + return $this->each->append($attributes); + } + /** * Get a dictionary keyed by primary keys. * diff --git a/tests/Database/DatabaseEloquentCollectionTest.php b/tests/Database/DatabaseEloquentCollectionTest.php index 17b731219c9c..7b01576bc83c 100755 --- a/tests/Database/DatabaseEloquentCollectionTest.php +++ b/tests/Database/DatabaseEloquentCollectionTest.php @@ -12,7 +12,7 @@ class DatabaseEloquentCollectionTest extends TestCase { - protected function tearDown(): void + protected function tearDown() : void { m::close(); } @@ -26,13 +26,13 @@ public function testAddingItemsToCollection() public function testGettingMaxItemsFromCollection() { - $c = new Collection([(object) ['foo' => 10], (object) ['foo' => 20]]); + $c = new Collection([(object)['foo' => 10], (object)['foo' => 20]]); $this->assertEquals(20, $c->max('foo')); } public function testGettingMinItemsFromCollection() { - $c = new Collection([(object) ['foo' => 10], (object) ['foo' => 20]]); + $c = new Collection([(object)['foo' => 10], (object)['foo' => 20]]); $this->assertEquals(10, $c->min('foo')); } @@ -114,10 +114,12 @@ public function testContainsClosureIndicatesIfModelInArray() $mockModel2->shouldReceive('getKey')->andReturn(2); $c = new Collection([$mockModel1, $mockModel2]); - $this->assertTrue($c->contains(function ($model) { + $this->assertTrue($c->contains(function ($model) + { return $model->getKey() < 2; })); - $this->assertFalse($c->contains(function ($model) { + $this->assertFalse($c->contains(function ($model) + { return $model->getKey() > 2; })); } @@ -209,7 +211,8 @@ public function testMap() $c = new Collection([$one, $two]); - $cAfterMap = $c->map(function ($item) { + $cAfterMap = $c->map(function ($item) + { return $item; }); @@ -222,7 +225,8 @@ public function testMappingToNonModelsReturnsABaseCollection() $one = m::mock(Model::class); $two = m::mock(Model::class); - $c = (new Collection([$one, $two]))->map(function ($item) { + $c = (new Collection([$one, $two]))->map(function ($item) + { return 'not-a-model'; }); @@ -387,6 +391,16 @@ public function testMakeVisibleRemovesHiddenFromEntireCollection() $this->assertEquals([], $c[0]->getHidden()); } + public function testAppendsAddsTestOnEntireCollection() + { + $c = new Collection([new TestEloquentCollectionModel]); + $c = $c->makeVisible('test'); + $c = $c->append('test'); + + $this->assertEquals(['test' => 'test'], $c[0]->toArray()); + + } + public function testNonModelRelatedMethods() { $a = new Collection([['foo' => 'bar'], ['foo' => 'baz']]); @@ -419,7 +433,7 @@ public function testQueueableCollectionImplementationThrowsExceptionOnMultipleMo $this->expectException(LogicException::class); $this->expectExceptionMessage('Queueing collections with multiple model types is not supported.'); - $c = new Collection([new TestEloquentCollectionModel, (object) ['id' => 'something']]); + $c = new Collection([new TestEloquentCollectionModel, (object)['id' => 'something']]); $c->getQueueableClass(); } @@ -434,4 +448,9 @@ class TestEloquentCollectionModel extends Model { protected $visible = ['visible']; protected $hidden = ['hidden']; + + public function getTestAttribute() + { + return "test"; + } }