Skip to content

Commit

Permalink
[11.x] Adds support for Attribute return mutators to the `Eloquent/Bu…
Browse files Browse the repository at this point in the history
…ilder` pluck method (#54130)

* [11.x] Adds support for Attribute return mutators to the `Builder` pluck method.

* [11.x] Adds a combined `hasAnyGetMutator` method for any mutator implementation.
  • Loading branch information
MattBradleyDev authored Jan 9, 2025
1 parent 6291298 commit 6a3145c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
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

0 comments on commit 6a3145c

Please sign in to comment.