Skip to content

Commit

Permalink
Optimize pluck() to avoid redundant column selection (#54396)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsocakave authored Jan 29, 2025
1 parent 1cd9bf4 commit 4a74d20
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3384,7 +3384,7 @@ public function pluck($column, $key = null)
// given columns / key. Once we have the results, we will be able to take
// the results and get the exact data that was requested for the query.
$queryResult = $this->onceWithColumns(
is_null($key) ? [$column] : [$column, $key],
is_null($key) || $key === $column ? [$column] : [$column, $key],
function () {
return $this->processor->processSelect(
$this, $this->runSelect()
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3364,6 +3364,17 @@ public function testPluckMethodGetsCollectionOfColumnValues()
$this->assertEquals([1 => 'bar', 10 => 'baz'], $results->all());
}

public function testPluckAvoidsDuplicateColumnSelection()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select "foo" from "users" where "id" = ?', [1], true)->andReturn([['foo' => 'bar']]);
$builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturnUsing(function ($query, $results) {
return $results;
});
$results = $builder->from('users')->where('id', '=', 1)->pluck('foo', 'foo');
$this->assertEquals(['bar' => 'bar'], $results->all());
}

public function testImplode()
{
// Test without glue.
Expand Down

0 comments on commit 4a74d20

Please sign in to comment.