Skip to content

[3.9] Respect Laravel 9's single word name mutators #2438

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

Merged
merged 8 commits into from
Sep 1, 2022
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
6 changes: 5 additions & 1 deletion src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
16 changes: 15 additions & 1 deletion tests/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'];
Expand Down Expand Up @@ -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)
);
}
}