diff --git a/src/Illuminate/Collections/helpers.php b/src/Illuminate/Collections/helpers.php index 67669e5ce1c6..5138b2cd1a12 100644 --- a/src/Illuminate/Collections/helpers.php +++ b/src/Illuminate/Collections/helpers.php @@ -58,7 +58,7 @@ function data_get($target, $key, $default = null) if ($segment === '*') { if ($target instanceof Collection) { $target = $target->all(); - } elseif (! is_array($target)) { + } elseif (! is_iterable($target)) { return value($default); } diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index bc2d71f19463..8acc3c7e7d00 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -3,9 +3,11 @@ namespace Illuminate\Tests\Support; use ArrayAccess; +use ArrayIterator; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Env; use Illuminate\Support\Optional; +use IteratorAggregate; use LogicException; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -87,10 +89,18 @@ public function testDataGetWithNestedArrays() ['name' => 'abigail'], ['name' => 'dayle'], ]; + $arrayIterable = new SupportTestArrayIterable([ + ['name' => 'taylor', 'email' => 'taylorotwell@gmail.com'], + ['name' => 'abigail'], + ['name' => 'dayle'], + ]); $this->assertEquals(['taylor', 'abigail', 'dayle'], data_get($array, '*.name')); $this->assertEquals(['taylorotwell@gmail.com', null, null], data_get($array, '*.email', 'irrelevant')); + $this->assertEquals(['taylor', 'abigail', 'dayle'], data_get($arrayIterable, '*.name')); + $this->assertEquals(['taylorotwell@gmail.com', null, null], data_get($arrayIterable, '*.email', 'irrelevant')); + $array = [ 'users' => [ ['first' => 'taylor', 'last' => 'otwell', 'email' => 'taylorotwell@gmail.com'], @@ -792,3 +802,18 @@ public function offsetUnset($offset) unset($this->attributes[$offset]); } } + +class SupportTestArrayIterable implements IteratorAggregate +{ + protected $items = []; + + public function __construct($items = []) + { + $this->items = $items; + } + + public function getIterator() + { + return new ArrayIterator($this->items); + } +}