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 all commits
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ public function pluck($column, $key = null)
// 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.
if (! $this->model->hasGetMutator($column) &&
if (! $this->model->hasAnyGetMutator($column) &&
! $this->model->hasCast($column) &&
! in_array($column, $this->model->getDates())) {
return $results;
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,17 @@ public function hasAttributeGetMutator($key)
return static::$getAttributeMutatorCache[get_class($this)][$key] = is_callable($this->{Str::camel($key)}()->get);
}

/**
* Determine if any get mutator exists for an attribute.
*
* @param string $key
* @return bool
*/
public function hasAnyGetMutator($key)
{
return $this->hasGetMutator($key) || $this->hasAttributeGetMutator($key);
}

/**
* Get the value of an attribute using its mutator.
*
Expand Down
14 changes: 7 additions & 7 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ public function testPluckReturnsTheMutatedAttributesOfAModel()
$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(true);
$builder->getModel()->shouldReceive('hasAnyGetMutator')->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 @@ -638,7 +638,7 @@ 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('hasAnyGetMutator')->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 @@ -651,7 +651,7 @@ public function testPluckReturnsTheDateAttributesOfAModel()
$builder = $this->getBuilder();
$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('hasAnyGetMutator')->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 @@ -668,7 +668,7 @@ public function testQualifiedPluckReturnsTheMutatedAttributesOfAModel()
$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('hasAnyGetMutator')->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 @@ -683,7 +683,7 @@ public function testQualifiedPluckReturnsTheCastedAttributesOfAModel()
$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('hasAnyGetMutator')->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 @@ -699,7 +699,7 @@ public function testQualifiedPluckReturnsTheDateAttributesOfAModel()
$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('hasAnyGetMutator')->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 @@ -713,7 +713,7 @@ public function testPluckWithoutModelGetterJustReturnsTheAttributesFoundInDataba
$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('hasAnyGetMutator')->with('name')->andReturn(false);
$builder->getModel()->shouldReceive('hasCast')->with('name')->andReturn(false);
$builder->getModel()->shouldReceive('getDates')->andReturn(['created_at']);

Expand Down