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

[5.4] Arr::pluck - handle situation when itemKey is an object #19838

Merged
merged 2 commits into from
Jun 30, 2017
Merged
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
4 changes: 4 additions & 0 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ public static function pluck($array, $value, $key = null)
} else {
$itemKey = data_get($item, $key);

if (gettype($itemKey) === 'object') {
$itemKey = (string) $itemKey;
}

$results[$itemKey] = $itemValue;
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use stdClass;
use ArrayObject;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use PHPUnit\Framework\TestCase;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -370,6 +371,15 @@ public function testPluckWithKeys()
], $test2);
}

public function testPluckWithCarbonKeys()
{
$array = [
['start' => new Carbon('2017-07-25 00:00:00'), 'end' => new Carbon('2017-07-30 00:00:00')],
];
$array = Arr::pluck($array, 'end', 'start');
$this->assertEquals(['2017-07-25 00:00:00' => '2017-07-30 00:00:00'], $array);
}

public function testPrepend()
{
$array = Arr::prepend(['one', 'two', 'three', 'four'], 'zero');
Expand Down