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

[12.x] Let Collection collect() method accept key to collect #55282

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion src/Illuminate/Collections/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1243,9 +1243,10 @@ public function zip($items);
/**
* Collect the values into a collection.
*
* @param string|null $key
* @return \Illuminate\Support\Collection<TKey, TValue>
*/
public function collect();
public function collect($key);

/**
* Get the collection of items as a plain array.
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,11 +926,12 @@ public function uniqueStrict($key = null)
/**
* Collect the values into a collection.
*
* @param string|null $key
* @return \Illuminate\Support\Collection<TKey, TValue>
*/
public function collect()
public function collect($key = null)
{
return new Collection($this->all());
return new Collection($key ? $this->get($key) : $this->all());
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5471,6 +5471,23 @@ public function testCollect($collection)
'b' => 2,
'c' => 3,
], $data->all());

$data = $collection::make([
'foos' => [
'foo',
'bar',
'baz',
],
'a' => 1,
])->collect('foos');

$this->assertInstanceOf(Collection::class, $data);

$this->assertSame([
'foo',
'bar',
'baz',
], $data->all());
}

#[DataProvider('collectionClassProvider')]
Expand Down
Loading