Skip to content

Fix Query\Builder::pluck() with ObjectId as key #3169

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

Merged
merged 3 commits into from
Oct 4, 2024
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
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
Loading