Skip to content

Conversation

JosephSilber
Copy link
Contributor

This has been requested a lot, and for a long time. It's immensely useful in countless situations.

To take just one example:

$tags = User::with('tags.posts.comments')->find(1)->tags;

$comments = $tags->pluck('posts.*.comments')->collapse()->unique();

This would have previously required way more code and/or an additional query1.

You can go as far as you need, with as many stars as you need:

$colors = $users->pluck('orders.*.items.*.product.colors')->collapse()->unique();

There are too many use cases to list here. This really puts data_get and array_pluck on steroids!


1 That excellent article is what actually reminded me about this. See the comments there.

@JosephSilber JosephSilber changed the title Add support for nested indexed arrays to array_pluck and data_get [5.1] Add support for nested indexed arrays to array_pluck and data_get Apr 24, 2015
@JosephSilber JosephSilber force-pushed the data-get-nested-array branch from 9f34564 to 0139c1d Compare April 24, 2015 04:16
@taylorotwell
Copy link
Member

Is this a breaking change if someone actually uses * as an array key and then tries to access that value?

@JosephSilber
Copy link
Contributor Author

It is. It's not a very common scenario, but it's not BC in that instance.

@taylorotwell
Copy link
Member

Going to hold off on this for now.

@JosephSilber JosephSilber deleted the data-get-nested-array branch May 19, 2015 17:58
@tobz-nz
Copy link
Contributor

tobz-nz commented Oct 7, 2015

+1 on this. Would be super useful.
Also, if you're putting *'s in you're array keys, you're probably doing something wrong.

@Riesjart2
Copy link
Contributor

+1

@Gummibeer
Copy link
Contributor

👍

@vlakoff
Copy link
Contributor

vlakoff commented Mar 3, 2016

Looks like we actually don't support more than 2 wildcards, because of the return:

if ($segment === '*') {
    ...
    return in_array('*', $key) ? Arr::collapse($result) : $result;
}

@vlakoff
Copy link
Contributor

vlakoff commented Mar 4, 2016

Mmm, but Arr::pluck calls data_get so it should be recursive.

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.

7 participants