Skip to content

Commit a964156

Browse files
fuyuki0511GromNaN
andauthored
Fix Query\Builder::pluck() with ObjectId as key (#3169)
Conversion of ObjectId to string is done in Laravel https://github.com/laravel/framework/blob/646520ad682d98b5211c6e26092259cfbe130b5c/src/Illuminate/Collections/Arr.php#L562 --------- Co-authored-by: Jérôme Tamarelle <[email protected]>
1 parent a5ef5c0 commit a964156

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/Query/Builder.php

-9
Original file line numberDiff line numberDiff line change
@@ -851,15 +851,6 @@ public function pluck($column, $key = null)
851851
{
852852
$results = $this->get($key === null ? [$column] : [$column, $key]);
853853

854-
// Convert ObjectID's to strings
855-
if (((string) $key) === '_id') {
856-
$results = $results->map(function ($item) {
857-
$item['_id'] = (string) $item['_id'];
858-
859-
return $item;
860-
});
861-
}
862-
863854
$p = Arr::pluck($results, $column, $key);
864855

865856
return new Collection($p);

tests/QueryBuilderTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,17 @@ public function testPluck()
525525
$this->assertEquals([25], $age);
526526
}
527527

528+
public function testPluckObjectId()
529+
{
530+
$id = new ObjectId();
531+
DB::table('users')->insert([
532+
['id' => $id, 'name' => 'Jane Doe'],
533+
]);
534+
535+
$names = DB::table('users')->pluck('name', 'id')->toArray();
536+
$this->assertEquals([(string) $id => 'Jane Doe'], $names);
537+
}
538+
528539
public function testList()
529540
{
530541
DB::table('items')->insert([

0 commit comments

Comments
 (0)