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

Conversation

ralphjsmit
Copy link
Contributor

@ralphjsmit ralphjsmit commented Apr 5, 2025

Sometimes it can be very handy when working with collections if you could select a key and get another collection of that key. This PR allows the ->collect() to accept a key on Collections.

$collection = collect([
   'foos' => ['foo', 'bar', 'baz'],
   'other-keys' => 'foo',
]);

$foos = $collection->collect('foos');
// Collection [foo/bar/baz]

Thanks!

PS: I am not sure if this is considered a breaking change because of the change in the Enumerable contract, or if this contract is only supposed for internal use.

@rodrigopedra
Copy link
Contributor

What happens in this case?

$collection = collect([
   'foos' => ['foo', 'bar', 'baz'],
   'other-keys' => 'foo',
   'user' => User::find(1),
]);

$foos = $collection->collect('user');
// ???

As the user key only has an Illuminate\Contracts\Support\Arrayable element, is the result something like this?

new Collection([User::find(1)])

Or something like this?

new Collection(User::find(1)->toArray())
// == new Collection(['name' => ..., 'email => ..., ...])

An option to work around this case would be:

return new Collection($key ? Arr::wrap($this->get($key)) : $this->all());

Regarding this:

I am not sure if this is considered a breaking change because of the change in the Enumerable contract, or if this contract is only supposed for internal use.

All classes, interfaces, methods and properties meant only for internal use, are marked with an @internal tag on their docblock.

So it is a breaking change, as it changes both an interface, and a class' public method signature, and should be sent to the master branch.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@ralphjsmit
Copy link
Contributor Author

@rodrigopedra Thank you for your comments! I see the PR is is closed, but to shortly answer your points:

The result of your example of $collection->pluck('user'), where user being an Eloquent model, would be the same as doing new Collection($user), thus resulting in an collection with the attribute keys as keys and the values as values. This is because all Eloquent models are Arrayable, and therefore the get arrayable items in the __construct() method results the array of attributes as source of the new collection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants