File tree 2 files changed +33
-1
lines changed
2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 6
6
use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
7
7
use Illuminate \Database \Eloquent \ModelNotFoundException ;
8
8
use Illuminate \Support \Facades \Date ;
9
+ use Illuminate \Support \Str ;
9
10
use Jenssegers \Mongodb \Collection ;
10
11
use Jenssegers \Mongodb \Connection ;
11
12
use Jenssegers \Mongodb \Eloquent \Model ;
@@ -678,6 +679,23 @@ public function testDotNotation(): void
678
679
$ this ->assertEquals ('Strasbourg ' , $ user ['address.city ' ]);
679
680
}
680
681
682
+ public function testAttributeMutator (): void
683
+ {
684
+ $ username = 'JaneDoe ' ;
685
+ $ usernameSlug = Str::slug ($ username );
686
+ $ user = User::create ([
687
+ 'name ' => 'Jane Doe ' ,
688
+ 'username ' => $ username ,
689
+ ]);
690
+
691
+ $ this ->assertNotEquals ($ username , $ user ->getAttribute ('username ' ));
692
+ $ this ->assertNotEquals ($ username , $ user ['username ' ]);
693
+ $ this ->assertNotEquals ($ username , $ user ->username );
694
+ $ this ->assertEquals ($ usernameSlug , $ user ->getAttribute ('username ' ));
695
+ $ this ->assertEquals ($ usernameSlug , $ user ['username ' ]);
696
+ $ this ->assertEquals ($ usernameSlug , $ user ->username );
697
+ }
698
+
681
699
public function testMultipleLevelDotNotation (): void
682
700
{
683
701
/** @var Book $book */
Original file line number Diff line number Diff line change 7
7
use Illuminate \Contracts \Auth \Authenticatable as AuthenticatableContract ;
8
8
use Illuminate \Contracts \Auth \CanResetPassword as CanResetPasswordContract ;
9
9
use Illuminate \Notifications \Notifiable ;
10
+ use Illuminate \Support \Str ;
10
11
use Jenssegers \Mongodb \Eloquent \HybridRelations ;
11
12
use Jenssegers \Mongodb \Eloquent \Model as Eloquent ;
13
+ use Illuminate \Database \Eloquent \Casts \Attribute ;
12
14
13
15
/**
14
16
* Class User.
21
23
* @property \Carbon\Carbon $birthday
22
24
* @property \Carbon\Carbon $created_at
23
25
* @property \Carbon\Carbon $updated_at
26
+ * @property string $username
24
27
*/
25
28
class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract
26
29
{
27
- use Authenticatable, CanResetPassword, HybridRelations, Notifiable;
30
+ use Authenticatable;
31
+ use CanResetPassword;
32
+ use HybridRelations;
33
+ use Notifiable;
28
34
29
35
protected $ connection = 'mongodb ' ;
30
36
protected $ dates = ['birthday ' , 'entry.date ' ];
@@ -84,4 +90,12 @@ protected function serializeDate(DateTimeInterface $date)
84
90
{
85
91
return $ date ->format ('l jS \of F Y h:i:s A ' );
86
92
}
93
+
94
+ protected function username (): Attribute
95
+ {
96
+ return Attribute::make (
97
+ get: fn ($ value ) => $ value ,
98
+ set: fn ($ value ) => Str::slug ($ value )
99
+ );
100
+ }
87
101
}
You can’t perform that action at this time.
0 commit comments