Skip to content

Commit

Permalink
[9.x] Force countBy method in EloquentCollection to return base colle…
Browse files Browse the repository at this point in the history
…ction (#45663)

* Force countBy method in EloquentCollection to return base collection

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
jnoordsij and taylorotwell authored Jan 16, 2023
1 parent 23e27e1 commit dc78921
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
69 changes: 40 additions & 29 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,38 +582,14 @@ public function getDictionary($items = null)
*/

/**
* Get an array with the values of a given key.
*
* @param string|array<array-key, string> $value
* @param string|null $key
* @return \Illuminate\Support\Collection<array-key, mixed>
*/
public function pluck($value, $key = null)
{
return $this->toBase()->pluck($value, $key);
}

/**
* Get the keys of the collection items.
*
* @return \Illuminate\Support\Collection<int, TKey>
*/
public function keys()
{
return $this->toBase()->keys();
}

/**
* Zip the collection together with one or more arrays.
*
* @template TZipValue
* Count the number of items in the collection by a field or using a callback.
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TZipValue>|iterable<array-key, TZipValue> ...$items
* @return \Illuminate\Support\Collection<int, \Illuminate\Support\Collection<int, TModel|TZipValue>>
* @param (callable(TValue, TKey): mixed)|string|null $countBy
* @return \Illuminate\Support\Collection<array-key, int>
*/
public function zip($items)
public function countBy($countBy = null)
{
return $this->toBase()->zip(...func_get_args());
return $this->toBase()->countBy($countBy);
}

/**
Expand Down Expand Up @@ -647,6 +623,16 @@ public function flip()
return $this->toBase()->flip();
}

/**
* Get the keys of the collection items.
*
* @return \Illuminate\Support\Collection<int, TKey>
*/
public function keys()
{
return $this->toBase()->keys();
}

/**
* Pad collection to the specified length with a value.
*
Expand All @@ -661,6 +647,31 @@ public function pad($size, $value)
return $this->toBase()->pad($size, $value);
}

/**
* Get an array with the values of a given key.
*
* @param string|array<array-key, string> $value
* @param string|null $key
* @return \Illuminate\Support\Collection<array-key, mixed>
*/
public function pluck($value, $key = null)
{
return $this->toBase()->pluck($value, $key);
}

/**
* Zip the collection together with one or more arrays.
*
* @template TZipValue
*
* @param \Illuminate\Contracts\Support\Arrayable<array-key, TZipValue>|iterable<array-key, TZipValue> ...$items
* @return \Illuminate\Support\Collection<int, \Illuminate\Support\Collection<int, TModel|TZipValue>>
*/
public function zip($items)
{
return $this->toBase()->zip(...func_get_args());
}

/**
* Get the comparison function to detect duplicates.
*
Expand Down
1 change: 1 addition & 0 deletions tests/Database/DatabaseEloquentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ public function testNonModelRelatedMethods()
$this->assertEquals(BaseCollection::class, get_class($a->collapse()));
$this->assertEquals(BaseCollection::class, get_class($a->flatten()));
$this->assertEquals(BaseCollection::class, get_class($a->zip(['a', 'b'], ['c', 'd'])));
$this->assertEquals(BaseCollection::class, get_class($a->countBy('foo')));
$this->assertEquals(BaseCollection::class, get_class($b->flip()));
}

Expand Down

0 comments on commit dc78921

Please sign in to comment.