Skip to content

Commit

Permalink
Added appends($attribute) method to Eloquent Collections
Browse files Browse the repository at this point in the history
  • Loading branch information
jazerix committed Apr 10, 2020
1 parent ac2045f commit 8053f6d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
35 changes: 27 additions & 8 deletions tests/Database/DatabaseEloquentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class DatabaseEloquentCollectionTest extends TestCase
{
protected function tearDown(): void
protected function tearDown() : void
{
m::close();
}
Expand All @@ -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'));
}

Expand Down Expand Up @@ -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;
}));
}
Expand Down Expand Up @@ -209,7 +211,8 @@ public function testMap()

$c = new Collection([$one, $two]);

$cAfterMap = $c->map(function ($item) {
$cAfterMap = $c->map(function ($item)
{
return $item;
});

Expand All @@ -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';
});

Expand Down Expand Up @@ -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']]);
Expand Down Expand Up @@ -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();
}

Expand All @@ -434,4 +448,9 @@ class TestEloquentCollectionModel extends Model
{
protected $visible = ['visible'];
protected $hidden = ['hidden'];

public function getTestAttribute()
{
return "test";
}
}

0 comments on commit 8053f6d

Please sign in to comment.