Skip to content

Commit

Permalink
[11.x] Fix pluck() casting with qualified column (#49288)
Browse files Browse the repository at this point in the history
* [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 <taylor@laravel.com>
  • Loading branch information
danharrin and taylorotwell authored Dec 13, 2023
1 parent 2b7a626 commit 562557f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@ public function pluck($column, $key = null)

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

$column = Str::after($column, "{$this->model->getTable()}.");

// If the model has a mutator for the requested column, we will spin through
// the results and mutate the values so that the mutated version of these
// columns are returned as you would expect from these Eloquent models.
Expand Down
48 changes: 48 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,54 @@ public function testPluckReturnsTheDateAttributesOfAModel()
$this->assertEquals(['date_2010-01-01 00:00:00', 'date_2011-01-01 00:00:00'], $builder->pluck('created_at')->all());
}

public function testQualifiedPluckReturnsTheMutatedAttributesOfAModel()
{
$model = $this->getMockModel();
$model->shouldReceive('qualifyColumn')->with('name')->andReturn('foo_table.name');

$builder = $this->getBuilder();
$builder->getQuery()->shouldReceive('pluck')->with($model->qualifyColumn('name'), '')->andReturn(new BaseCollection(['bar', 'baz']));
$builder->setModel($model);
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(true);
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'bar'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'bar']));
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'baz'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'baz']));

$this->assertEquals(['foo_bar', 'foo_baz'], $builder->pluck($model->qualifyColumn('name'))->all());
}

public function testQualifiedPluckReturnsTheCastedAttributesOfAModel()
{
$model = $this->getMockModel();
$model->shouldReceive('qualifyColumn')->with('name')->andReturn('foo_table.name');

$builder = $this->getBuilder();
$builder->getQuery()->shouldReceive('pluck')->with($model->qualifyColumn('name'), '')->andReturn(new BaseCollection(['bar', 'baz']));
$builder->setModel($model);
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(false);
$builder->getModel()->shouldReceive('hasCast')->with('name')->andReturn(true);
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'bar'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'bar']));
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'baz'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'baz']));

$this->assertEquals(['foo_bar', 'foo_baz'], $builder->pluck($model->qualifyColumn('name'))->all());
}

public function testQualifiedPluckReturnsTheDateAttributesOfAModel()
{
$model = $this->getMockModel();
$model->shouldReceive('qualifyColumn')->with('created_at')->andReturn('foo_table.created_at');

$builder = $this->getBuilder();
$builder->getQuery()->shouldReceive('pluck')->with($model->qualifyColumn('created_at'), '')->andReturn(new BaseCollection(['2010-01-01 00:00:00', '2011-01-01 00:00:00']));
$builder->setModel($model);
$builder->getModel()->shouldReceive('hasGetMutator')->with('created_at')->andReturn(false);
$builder->getModel()->shouldReceive('hasCast')->with('created_at')->andReturn(false);
$builder->getModel()->shouldReceive('getDates')->andReturn(['created_at']);
$builder->getModel()->shouldReceive('newFromBuilder')->with(['created_at' => '2010-01-01 00:00:00'])->andReturn(new EloquentBuilderTestPluckDatesStub(['created_at' => '2010-01-01 00:00:00']));
$builder->getModel()->shouldReceive('newFromBuilder')->with(['created_at' => '2011-01-01 00:00:00'])->andReturn(new EloquentBuilderTestPluckDatesStub(['created_at' => '2011-01-01 00:00:00']));

$this->assertEquals(['date_2010-01-01 00:00:00', 'date_2011-01-01 00:00:00'], $builder->pluck($model->qualifyColumn('created_at'))->all());
}

public function testPluckWithoutModelGetterJustReturnsTheAttributesFoundInDatabase()
{
$builder = $this->getBuilder();
Expand Down
8 changes: 2 additions & 6 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,14 +931,10 @@ public function testCanTouchRelatedModels()
$post->tags()->touch();

foreach ($post->tags()->pluck('tags.updated_at') as $date) {
if ($this->driver === 'sqlsrv') {
$this->assertSame('2017-10-10 10:10:10.000', $date);
} else {
$this->assertSame('2017-10-10 10:10:10', $date);
}
$this->assertSame('2017-10-10 10:10:10', $date->toDateTimeString());
}

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

public function testWherePivotOnString()
Expand Down

0 comments on commit 562557f

Please sign in to comment.