diff --git a/src/Eloquent/DocumentModel.php b/src/Eloquent/DocumentModel.php index 1ce62c733..3f3829ede 100644 --- a/src/Eloquent/DocumentModel.php +++ b/src/Eloquent/DocumentModel.php @@ -177,7 +177,7 @@ public function getAttribute($key) method_exists($this, $key) && ! method_exists(Model::class, $key) && ! method_exists(DocumentModel::class, $key) - && ! $this->hasAttributeGetMutator($key) + && ! $this->hasAttributeMutator($key) ) { return $this->getRelationValue($key); } diff --git a/tests/Models/HiddenAnimal.php b/tests/Models/HiddenAnimal.php index f6217177c..05d93e7c2 100644 --- a/tests/Models/HiddenAnimal.php +++ b/tests/Models/HiddenAnimal.php @@ -4,6 +4,7 @@ namespace MongoDB\Laravel\Tests\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use MongoDB\Laravel\Eloquent\DocumentModel; @@ -11,6 +12,7 @@ * @property string $name * @property string $country * @property bool $can_be_eaten + * @property string $secret */ final class HiddenAnimal extends Model { @@ -21,7 +23,19 @@ final class HiddenAnimal extends Model 'name', 'country', 'can_be_eaten', + 'secret', ]; - protected $hidden = ['country']; + protected $hidden = ['country', 'secret']; + + /** + * Reproduces laravel/passport Client::secret(): a set-only Attribute mutator + * whose method name collides with a hidden attribute. + */ + protected function secret(): Attribute + { + return Attribute::make( + set: fn (?string $value): ?string => $value, + ); + } } diff --git a/tests/PropertyTest.php b/tests/PropertyTest.php index 67153006b..b97193b23 100644 --- a/tests/PropertyTest.php +++ b/tests/PropertyTest.php @@ -34,4 +34,21 @@ public function testCanHideCertainProperties(): void self::assertArrayNotHasKey('country', $hiddenAnimal->toArray(), 'the country column should be hidden'); self::assertArrayHasKey('can_be_eaten', $hiddenAnimal->toArray()); } + + public function testHiddenAttributeWithSetOnlyAttributeMutatorIsAccessible(): void + { + HiddenAnimal::create([ + 'name' => 'Sheep', + 'country' => 'Ireland', + 'can_be_eaten' => true, + 'secret' => 'shhh', + ]); + + $hiddenAnimal = HiddenAnimal::sole(); + assert($hiddenAnimal instanceof HiddenAnimal); + + self::assertSame('shhh', $hiddenAnimal->secret); + self::assertSame('shhh', $hiddenAnimal->getAttributes()['secret']); + self::assertArrayNotHasKey('secret', $hiddenAnimal->toArray()); + } }