Skip to content
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

[11.x] Adds support for Attribute return mutators to the Eloquent/Builder pluck method #54130

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ public function pluck($column, $key = null)
// the results and mutate the values so that the mutated version of these
// columns are returned as you would expect from these Eloquent models.
if (! $this->model->hasGetMutator($column) &&
! $this->model->hasAttributeGetMutator($column) &&
MattBradleyDev marked this conversation as resolved.
Show resolved Hide resolved
! $this->model->hasCast($column) &&
! in_array($column, $this->model->getDates())) {
return $results;
Expand Down
34 changes: 34 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,12 +633,26 @@ public function testPluckReturnsTheMutatedAttributesOfAModel()
$this->assertEquals(['foo_bar', 'foo_baz'], $builder->pluck('name')->all());
}

public function testPluckReturnsTheMutatedAttributesOfAModelWithAttributeReturn()
{
$builder = $this->getBuilder();
$builder->getQuery()->shouldReceive('pluck')->with('name', '')->andReturn(new BaseCollection(['bar', 'baz']));
$builder->setModel($this->getMockModel());
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(false);
$builder->getModel()->shouldReceive('hasAttributeGetMutator')->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('name')->all());
}

public function testPluckReturnsTheCastedAttributesOfAModel()
{
$builder = $this->getBuilder();
$builder->getQuery()->shouldReceive('pluck')->with('name', '')->andReturn(new BaseCollection(['bar', 'baz']));
$builder->setModel($this->getMockModel());
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(false);
$builder->getModel()->shouldReceive('hasAttributeGetMutator')->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']));
Expand All @@ -652,6 +666,7 @@ public function testPluckReturnsTheDateAttributesOfAModel()
$builder->getQuery()->shouldReceive('pluck')->with('created_at', '')->andReturn(new BaseCollection(['2010-01-01 00:00:00', '2011-01-01 00:00:00']));
$builder->setModel($this->getMockModel());
$builder->getModel()->shouldReceive('hasGetMutator')->with('created_at')->andReturn(false);
$builder->getModel()->shouldReceive('hasAttributeGetMutator')->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']));
Expand All @@ -675,6 +690,22 @@ public function testQualifiedPluckReturnsTheMutatedAttributesOfAModel()
$this->assertEquals(['foo_bar', 'foo_baz'], $builder->pluck($model->qualifyColumn('name'))->all());
}

public function testQualifiedPluckReturnsTheMutatedAttributesOfAModelWithAttributeReturn()
{
$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('hasAttributeGetMutator')->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();
Expand All @@ -684,6 +715,7 @@ public function testQualifiedPluckReturnsTheCastedAttributesOfAModel()
$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('hasAttributeGetMutator')->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']));
Expand All @@ -700,6 +732,7 @@ public function testQualifiedPluckReturnsTheDateAttributesOfAModel()
$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('hasAttributeGetMutator')->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']));
Expand All @@ -714,6 +747,7 @@ public function testPluckWithoutModelGetterJustReturnsTheAttributesFoundInDataba
$builder->getQuery()->shouldReceive('pluck')->with('name', '')->andReturn(new BaseCollection(['bar', 'baz']));
$builder->setModel($this->getMockModel());
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(false);
$builder->getModel()->shouldReceive('hasAttributeGetMutator')->with('name')->andReturn(false);
$builder->getModel()->shouldReceive('hasCast')->with('name')->andReturn(false);
$builder->getModel()->shouldReceive('getDates')->andReturn(['created_at']);

Expand Down
Loading