Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Force countBy method in EloquentCollection to return base collection #45663

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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