Skip to content

Commit

Permalink
Fix Query\Builder::pluck() with ObjectId as key (#3169)
Browse files Browse the repository at this point in the history
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 <jerome.tamarelle@mongodb.com>
  • Loading branch information
fuyuki0511 and GromNaN authored Oct 4, 2024
1 parent a5ef5c0 commit a964156
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 0 additions & 9 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,15 +851,6 @@ public function pluck($column, $key = null)
{
$results = $this->get($key === null ? [$column] : [$column, $key]);

// Convert ObjectID's to strings
if (((string) $key) === '_id') {
$results = $results->map(function ($item) {
$item['_id'] = (string) $item['_id'];

return $item;
});
}

$p = Arr::pluck($results, $column, $key);

return new Collection($p);
Expand Down
11 changes: 11 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,17 @@ public function testPluck()
$this->assertEquals([25], $age);
}

public function testPluckObjectId()
{
$id = new ObjectId();
DB::table('users')->insert([
['id' => $id, 'name' => 'Jane Doe'],
]);

$names = DB::table('users')->pluck('name', 'id')->toArray();
$this->assertEquals([(string) $id => 'Jane Doe'], $names);
}

public function testList()
{
DB::table('items')->insert([
Expand Down

0 comments on commit a964156

Please sign in to comment.