Skip to content

Commit 6a3145c

Browse files
[11.x] Adds support for Attribute return mutators to the Eloquent/Builder 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.
1 parent 6291298 commit 6a3145c

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ public function pluck($column, $key = null)
947947
// If the model has a mutator for the requested column, we will spin through
948948
// the results and mutate the values so that the mutated version of these
949949
// columns are returned as you would expect from these Eloquent models.
950-
if (! $this->model->hasGetMutator($column) &&
950+
if (! $this->model->hasAnyGetMutator($column) &&
951951
! $this->model->hasCast($column) &&
952952
! in_array($column, $this->model->getDates())) {
953953
return $results;

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,17 @@ public function hasAttributeGetMutator($key)
677677
return static::$getAttributeMutatorCache[get_class($this)][$key] = is_callable($this->{Str::camel($key)}()->get);
678678
}
679679

680+
/**
681+
* Determine if any get mutator exists for an attribute.
682+
*
683+
* @param string $key
684+
* @return bool
685+
*/
686+
public function hasAnyGetMutator($key)
687+
{
688+
return $this->hasGetMutator($key) || $this->hasAttributeGetMutator($key);
689+
}
690+
680691
/**
681692
* Get the value of an attribute using its mutator.
682693
*

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ public function testPluckReturnsTheMutatedAttributesOfAModel()
626626
$builder = $this->getBuilder();
627627
$builder->getQuery()->shouldReceive('pluck')->with('name', '')->andReturn(new BaseCollection(['bar', 'baz']));
628628
$builder->setModel($this->getMockModel());
629-
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(true);
629+
$builder->getModel()->shouldReceive('hasAnyGetMutator')->with('name')->andReturn(true);
630630
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'bar'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'bar']));
631631
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'baz'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'baz']));
632632

@@ -638,7 +638,7 @@ public function testPluckReturnsTheCastedAttributesOfAModel()
638638
$builder = $this->getBuilder();
639639
$builder->getQuery()->shouldReceive('pluck')->with('name', '')->andReturn(new BaseCollection(['bar', 'baz']));
640640
$builder->setModel($this->getMockModel());
641-
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(false);
641+
$builder->getModel()->shouldReceive('hasAnyGetMutator')->with('name')->andReturn(false);
642642
$builder->getModel()->shouldReceive('hasCast')->with('name')->andReturn(true);
643643
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'bar'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'bar']));
644644
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'baz'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'baz']));
@@ -651,7 +651,7 @@ public function testPluckReturnsTheDateAttributesOfAModel()
651651
$builder = $this->getBuilder();
652652
$builder->getQuery()->shouldReceive('pluck')->with('created_at', '')->andReturn(new BaseCollection(['2010-01-01 00:00:00', '2011-01-01 00:00:00']));
653653
$builder->setModel($this->getMockModel());
654-
$builder->getModel()->shouldReceive('hasGetMutator')->with('created_at')->andReturn(false);
654+
$builder->getModel()->shouldReceive('hasAnyGetMutator')->with('created_at')->andReturn(false);
655655
$builder->getModel()->shouldReceive('hasCast')->with('created_at')->andReturn(false);
656656
$builder->getModel()->shouldReceive('getDates')->andReturn(['created_at']);
657657
$builder->getModel()->shouldReceive('newFromBuilder')->with(['created_at' => '2010-01-01 00:00:00'])->andReturn(new EloquentBuilderTestPluckDatesStub(['created_at' => '2010-01-01 00:00:00']));
@@ -668,7 +668,7 @@ public function testQualifiedPluckReturnsTheMutatedAttributesOfAModel()
668668
$builder = $this->getBuilder();
669669
$builder->getQuery()->shouldReceive('pluck')->with($model->qualifyColumn('name'), '')->andReturn(new BaseCollection(['bar', 'baz']));
670670
$builder->setModel($model);
671-
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(true);
671+
$builder->getModel()->shouldReceive('hasAnyGetMutator')->with('name')->andReturn(true);
672672
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'bar'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'bar']));
673673
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'baz'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'baz']));
674674

@@ -683,7 +683,7 @@ public function testQualifiedPluckReturnsTheCastedAttributesOfAModel()
683683
$builder = $this->getBuilder();
684684
$builder->getQuery()->shouldReceive('pluck')->with($model->qualifyColumn('name'), '')->andReturn(new BaseCollection(['bar', 'baz']));
685685
$builder->setModel($model);
686-
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(false);
686+
$builder->getModel()->shouldReceive('hasAnyGetMutator')->with('name')->andReturn(false);
687687
$builder->getModel()->shouldReceive('hasCast')->with('name')->andReturn(true);
688688
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'bar'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'bar']));
689689
$builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'baz'])->andReturn(new EloquentBuilderTestPluckStub(['name' => 'baz']));
@@ -699,7 +699,7 @@ public function testQualifiedPluckReturnsTheDateAttributesOfAModel()
699699
$builder = $this->getBuilder();
700700
$builder->getQuery()->shouldReceive('pluck')->with($model->qualifyColumn('created_at'), '')->andReturn(new BaseCollection(['2010-01-01 00:00:00', '2011-01-01 00:00:00']));
701701
$builder->setModel($model);
702-
$builder->getModel()->shouldReceive('hasGetMutator')->with('created_at')->andReturn(false);
702+
$builder->getModel()->shouldReceive('hasAnyGetMutator')->with('created_at')->andReturn(false);
703703
$builder->getModel()->shouldReceive('hasCast')->with('created_at')->andReturn(false);
704704
$builder->getModel()->shouldReceive('getDates')->andReturn(['created_at']);
705705
$builder->getModel()->shouldReceive('newFromBuilder')->with(['created_at' => '2010-01-01 00:00:00'])->andReturn(new EloquentBuilderTestPluckDatesStub(['created_at' => '2010-01-01 00:00:00']));
@@ -713,7 +713,7 @@ public function testPluckWithoutModelGetterJustReturnsTheAttributesFoundInDataba
713713
$builder = $this->getBuilder();
714714
$builder->getQuery()->shouldReceive('pluck')->with('name', '')->andReturn(new BaseCollection(['bar', 'baz']));
715715
$builder->setModel($this->getMockModel());
716-
$builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(false);
716+
$builder->getModel()->shouldReceive('hasAnyGetMutator')->with('name')->andReturn(false);
717717
$builder->getModel()->shouldReceive('hasCast')->with('name')->andReturn(false);
718718
$builder->getModel()->shouldReceive('getDates')->andReturn(['created_at']);
719719

0 commit comments

Comments
 (0)