-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Description
For data_get function I can pass string|array as $key & the $default = null https://github.com/laravel/framework/blob/5.2/src/Illuminate/Support/helpers.php#L401-L405. Now I have a collection
$collection = collect(['title' => 'Lean Startup', 'category' => 'story', 'price' => 10]);
$result = data_get($collection, ['title', 'category']);//I want to get title & category from my collection
Now if you data_get function code it first extracts the keys & loops through it.
When we have key of * it returns all but if we a single key or a string of key it returns ok. But if we have $key with multiple values it returns wrong the default value because every time assigns the value
if (Arr::accessible($target) && Arr::exists($target, $segment)) {
$target = $target[$segment];//assigns value
} elseif (is_object($target) && isset($target->{$segment})) {
$target = $target->{$segment};//assigns value
} else {
return value($default);
}
So at first though $target is
Illuminate\Support\Collection {#729
all: [
"title" => "Lean Startup",
"category" => "story",
"price" => 10,
],
}
when $key is title the $target is replaced $target = $target[$segment]; & here $target is string & it's value is "Lean Startup" so in the next iteration (Arr::accessible($target) && Arr::exists($target, $segment)) or (is_object($target) && isset($target->{$segment})) neither condition satisfies as $target = 'Lean Startup' so it returns $default value which is null.