Skip to content

Commit 562557f

Browse files
[11.x] Fix pluck() casting with qualified column (#49288)
* [10.x]: Fix pluck() casting with qualified column * Fix failing test * Update EloquentBelongsToManyTest.php * Update tests/Integration/Database/EloquentBelongsToManyTest.php * Update Builder.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 2b7a626 commit 562557f

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ public function pluck($column, $key = null)
888888

889889
$column = $column instanceof Expression ? $column->getValue($this->getGrammar()) : $column;
890890

891+
$column = Str::after($column, "{$this->model->getTable()}.");
892+
891893
// If the model has a mutator for the requested column, we will spin through
892894
// the results and mutate the values so that the mutated version of these
893895
// columns are returned as you would expect from these Eloquent models.

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,54 @@ public function testPluckReturnsTheDateAttributesOfAModel()
659659
$this->assertEquals(['date_2010-01-01 00:00:00', 'date_2011-01-01 00:00:00'], $builder->pluck('created_at')->all());
660660
}
661661

662+
public function testQualifiedPluckReturnsTheMutatedAttributesOfAModel()
663+
{
664+
$model = $this->getMockModel();
665+
$model->shouldReceive('qualifyColumn')->with('name')->andReturn('foo_table.name');
666+
667+
$builder = $this->getBuilder();
668+
$builder->getQuery()->shouldReceive('pluck')->with($model->qualifyColumn('name'), '')->andReturn(new BaseCollection(['bar', 'baz']));
669+
$builder->setModel($model);
670+
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(true);
671+
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'bar'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'bar']));
672+
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'baz'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'baz']));
673+
674+
$this->assertEquals(['foo_bar', 'foo_baz'], $builder->pluck($model->qualifyColumn('name'))->all());
675+
}
676+
677+
public function testQualifiedPluckReturnsTheCastedAttributesOfAModel()
678+
{
679+
$model = $this->getMockModel();
680+
$model->shouldReceive('qualifyColumn')->with('name')->andReturn('foo_table.name');
681+
682+
$builder = $this->getBuilder();
683+
$builder->getQuery()->shouldReceive('pluck')->with($model->qualifyColumn('name'), '')->andReturn(new BaseCollection(['bar', 'baz']));
684+
$builder->setModel($model);
685+
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(false);
686+
$builder->getModel()->shouldReceive('hasCast')->with('name')->andReturn(true);
687+
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'bar'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'bar']));
688+
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'baz'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'baz']));
689+
690+
$this->assertEquals(['foo_bar', 'foo_baz'], $builder->pluck($model->qualifyColumn('name'))->all());
691+
}
692+
693+
public function testQualifiedPluckReturnsTheDateAttributesOfAModel()
694+
{
695+
$model = $this->getMockModel();
696+
$model->shouldReceive('qualifyColumn')->with('created_at')->andReturn('foo_table.created_at');
697+
698+
$builder = $this->getBuilder();
699+
$builder->getQuery()->shouldReceive('pluck')->with($model->qualifyColumn('created_at'), '')->andReturn(new BaseCollection(['2010-01-01 00:00:00', '2011-01-01 00:00:00']));
700+
$builder->setModel($model);
701+
$builder->getModel()->shouldReceive('hasGetMutator')->with('created_at')->andReturn(false);
702+
$builder->getModel()->shouldReceive('hasCast')->with('created_at')->andReturn(false);
703+
$builder->getModel()->shouldReceive('getDates')->andReturn(['created_at']);
704+
$builder->getModel()->shouldReceive('newFromBuilder')->with(['created_at' => '2010-01-01 00:00:00'])->andReturn(new EloquentBuilderTestPluckDatesStub(['created_at' => '2010-01-01 00:00:00']));
705+
$builder->getModel()->shouldReceive('newFromBuilder')->with(['created_at' => '2011-01-01 00:00:00'])->andReturn(new EloquentBuilderTestPluckDatesStub(['created_at' => '2011-01-01 00:00:00']));
706+
707+
$this->assertEquals(['date_2010-01-01 00:00:00', 'date_2011-01-01 00:00:00'], $builder->pluck($model->qualifyColumn('created_at'))->all());
708+
}
709+
662710
public function testPluckWithoutModelGetterJustReturnsTheAttributesFoundInDatabase()
663711
{
664712
$builder = $this->getBuilder();

tests/Integration/Database/EloquentBelongsToManyTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -931,14 +931,10 @@ public function testCanTouchRelatedModels()
931931
$post->tags()->touch();
932932

933933
foreach ($post->tags()->pluck('tags.updated_at') as $date) {
934-
if ($this->driver === 'sqlsrv') {
935-
$this->assertSame('2017-10-10 10:10:10.000', $date);
936-
} else {
937-
$this->assertSame('2017-10-10 10:10:10', $date);
938-
}
934+
$this->assertSame('2017-10-10 10:10:10', $date->toDateTimeString());
939935
}
940936

941-
$this->assertNotSame('2017-10-10 10:10:10', Tag::find(2)->updated_at);
937+
$this->assertNotSame('2017-10-10 10:10:10', Tag::find(2)->updated_at?->toDateTimeString());
942938
}
943939

944940
public function testWherePivotOnString()

0 commit comments

Comments
 (0)