diff --git a/src/Eloquent/Model.php b/src/Eloquent/Model.php index 226bc357d..5836cf83d 100644 --- a/src/Eloquent/Model.php +++ b/src/Eloquent/Model.php @@ -155,7 +155,11 @@ public function getAttribute($key) } // This checks for embedded relation support. - if (method_exists($this, $key) && ! method_exists(self::class, $key)) { + if ( + method_exists($this, $key) + && ! method_exists(self::class, $key) + && ! $this->hasAttributeGetMutator($key) + ) { return $this->getRelationValue($key); } diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 75723c1cb..9e8f60fea 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\Date; +use Illuminate\Support\Str; use Jenssegers\Mongodb\Collection; use Jenssegers\Mongodb\Connection; use Jenssegers\Mongodb\Eloquent\Model; @@ -678,6 +679,23 @@ public function testDotNotation(): void $this->assertEquals('Strasbourg', $user['address.city']); } + public function testAttributeMutator(): void + { + $username = 'JaneDoe'; + $usernameSlug = Str::slug($username); + $user = User::create([ + 'name' => 'Jane Doe', + 'username' => $username, + ]); + + $this->assertNotEquals($username, $user->getAttribute('username')); + $this->assertNotEquals($username, $user['username']); + $this->assertNotEquals($username, $user->username); + $this->assertEquals($usernameSlug, $user->getAttribute('username')); + $this->assertEquals($usernameSlug, $user['username']); + $this->assertEquals($usernameSlug, $user->username); + } + public function testMultipleLevelDotNotation(): void { /** @var Book $book */ diff --git a/tests/models/User.php b/tests/models/User.php index ff96b89e4..b394ea6e7 100644 --- a/tests/models/User.php +++ b/tests/models/User.php @@ -6,7 +6,9 @@ use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Str; use Jenssegers\Mongodb\Eloquent\HybridRelations; use Jenssegers\Mongodb\Eloquent\Model as Eloquent; @@ -21,10 +23,14 @@ * @property \Carbon\Carbon $birthday * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at + * @property string $username */ class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract { - use Authenticatable, CanResetPassword, HybridRelations, Notifiable; + use Authenticatable; + use CanResetPassword; + use HybridRelations; + use Notifiable; protected $connection = 'mongodb'; protected $dates = ['birthday', 'entry.date']; @@ -84,4 +90,12 @@ protected function serializeDate(DateTimeInterface $date) { return $date->format('l jS \of F Y h:i:s A'); } + + protected function username(): Attribute + { + return Attribute::make( + get: fn ($value) => $value, + set: fn ($value) => Str::slug($value) + ); + } }