44
55namespace App \Models ;
66
7+ use Carbon \CarbonImmutable ;
78use Database \Factories \UserFactory ;
9+ use Eloquent ;
810use Illuminate \Contracts \Auth \MustVerifyEmail ;
11+ use Illuminate \Database \Eloquent \Builder ;
12+ use Illuminate \Database \Eloquent \Casts \Attribute ;
913use Illuminate \Database \Eloquent \Factories \HasFactory ;
1014use Illuminate \Foundation \Auth \User as Authenticatable ;
15+ use Illuminate \Notifications \DatabaseNotification ;
16+ use Illuminate \Notifications \DatabaseNotificationCollection ;
1117use Illuminate \Notifications \Notifiable ;
18+ use Illuminate \Support \Facades \Storage ;
1219
20+ /**
21+ * @property int $id
22+ * @property string $first_name
23+ * @property string $last_name
24+ * @property string|null $avatar
25+ * @property string $email
26+ * @property CarbonImmutable|null $email_verified_at
27+ * @property string $password
28+ * @property string|null $remember_token
29+ * @property CarbonImmutable|null $created_at
30+ * @property CarbonImmutable|null $updated_at
31+ * @property-read DatabaseNotificationCollection<int, DatabaseNotification> $notifications
32+ * @property-read int|null $notifications_count
33+ *
34+ * @method static UserFactory factory($count = null, $state = [])
35+ * @method static Builder<static>|User newModelQuery()
36+ * @method static Builder<static>|User newQuery()
37+ * @method static Builder<static>|User query()
38+ * @method static Builder<static>|User whereAvatar($value)
39+ * @method static Builder<static>|User whereCreatedAt($value)
40+ * @method static Builder<static>|User whereEmail($value)
41+ * @method static Builder<static>|User whereEmailVerifiedAt($value)
42+ * @method static Builder<static>|User whereFirstName($value)
43+ * @method static Builder<static>|User whereId($value)
44+ * @method static Builder<static>|User whereLastName($value)
45+ * @method static Builder<static>|User wherePassword($value)
46+ * @method static Builder<static>|User whereRememberToken($value)
47+ * @method static Builder<static>|User whereUpdatedAt($value)
48+ *
49+ * @mixin Eloquent
50+ */
1351final class User extends Authenticatable implements MustVerifyEmail
1452{
1553 /** @use HasFactory<UserFactory> */
1654 use HasFactory, Notifiable;
1755
1856 /**
19- * The attributes that are mass assignable.
20- *
2157 * @var list<string>
2258 */
23- protected $ fillable = [
24- 'name ' ,
25- 'email ' ,
26- 'password ' ,
59+ protected $ appends = [
60+ 'full_name ' ,
61+ 'initials ' ,
62+ 'profile_image ' ,
2763 ];
2864
2965 /**
@@ -48,4 +84,35 @@ protected function casts(): array
4884 'password ' => 'hashed ' ,
4985 ];
5086 }
87+
88+ /**
89+ * @return Attribute<string, string>
90+ */
91+ protected function fullName (): Attribute
92+ {
93+ return Attribute::make (fn (): string => "$ this ->first_name $ this ->last_name " );
94+ }
95+
96+ /**
97+ * @return Attribute<string, string>
98+ */
99+ protected function initials (): Attribute
100+ {
101+ $ firstNameInitial = substr ($ this ->first_name ?? '' , 0 , 1 );
102+ $ lastNameInitial = substr ($ this ->last_name ?? '' , 0 , 1 );
103+
104+ return Attribute::make (fn (): string => $ firstNameInitial .$ lastNameInitial );
105+ }
106+
107+ /**
108+ * @return Attribute<string, string>
109+ */
110+ protected function profileImage (): Attribute
111+ {
112+ $ avatar = $ this ->avatar !== null
113+ ? Storage::url ($ this ->avatar )
114+ : null ;
115+
116+ return Attribute::make (fn (): ?string => $ avatar );
117+ }
51118}
0 commit comments