Skip to content

Commit 4a74d20

Browse files
authored
Optimize pluck() to avoid redundant column selection (#54396)
1 parent 1cd9bf4 commit 4a74d20

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3384,7 +3384,7 @@ public function pluck($column, $key = null)
33843384
// given columns / key. Once we have the results, we will be able to take
33853385
// the results and get the exact data that was requested for the query.
33863386
$queryResult = $this->onceWithColumns(
3387-
is_null($key) ? [$column] : [$column, $key],
3387+
is_null($key) || $key === $column ? [$column] : [$column, $key],
33883388
function () {
33893389
return $this->processor->processSelect(
33903390
$this, $this->runSelect()

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3364,6 +3364,17 @@ public function testPluckMethodGetsCollectionOfColumnValues()
33643364
$this->assertEquals([1 => 'bar', 10 => 'baz'], $results->all());
33653365
}
33663366

3367+
public function testPluckAvoidsDuplicateColumnSelection()
3368+
{
3369+
$builder = $this->getBuilder();
3370+
$builder->getConnection()->shouldReceive('select')->once()->with('select "foo" from "users" where "id" = ?', [1], true)->andReturn([['foo' => 'bar']]);
3371+
$builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturnUsing(function ($query, $results) {
3372+
return $results;
3373+
});
3374+
$results = $builder->from('users')->where('id', '=', 1)->pluck('foo', 'foo');
3375+
$this->assertEquals(['bar' => 'bar'], $results->all());
3376+
}
3377+
33673378
public function testImplode()
33683379
{
33693380
// Test without glue.

0 commit comments

Comments
 (0)