Skip to content

Commit ddbf3f5

Browse files
authored
add test and remove string convert
1 parent e51bc61 commit ddbf3f5

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Query/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ public function pluck($column, $key = null)
854854
// Convert ObjectID's to strings
855855
if (((string) $key) === '_id') {
856856
$results = $results->map(function ($item) {
857-
$item->_id = (string) $item->id;
857+
$item->_id = $item->id;
858858

859859
return $item;
860860
});

tests/Query/BuilderTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,4 +1573,28 @@ private static function getBuilder(): Builder
15731573

15741574
return new Builder($connection, null, $processor);
15751575
}
1576+
1577+
public function testPluckObjectId()
1578+
{
1579+
DB::table('users')->insert([
1580+
['_id' => $id = new \MongoDB\BSON\ObjectId(), 'name' => 'Jane Doe', 'age' => 20],
1581+
]);
1582+
1583+
$names = DB::table('users')->pluck('name', '_id')->toArray();
1584+
$this->assertCount(1, $names);
1585+
$this->assertArrayHasKey((string) $id, $names);
1586+
$this->assertEquals([(string) $id => 'Jane Doe'], $names);
1587+
}
1588+
1589+
public function testPluckObjectIdWithIdAlias()
1590+
{
1591+
DB::table('users')->insert([
1592+
['_id' => $id = new \MongoDB\BSON\ObjectId(), 'name' => 'Jane Doe', 'age' => 20],
1593+
]);
1594+
1595+
$names = DB::table('users')->pluck('name', 'id')->toArray();
1596+
$this->assertCount(1, $names);
1597+
$this->assertArrayHasKey((string) $id, $names);
1598+
$this->assertEquals([(string) $id => 'Jane Doe'], $names);
1599+
}
15761600
}

0 commit comments

Comments
 (0)