6
6
use App \Traits \Searchable ;
7
7
use Illuminate \Contracts \Auth \MustVerifyEmail ;
8
8
use Illuminate \Database \Eloquent \Factories \HasFactory ;
9
+ use Illuminate \Database \Eloquent \Relations \MorphMany ;
9
10
use Illuminate \Foundation \Auth \User as Authenticatable ;
11
+ use Illuminate \Foundation \Testing \Concerns \InteractsWithAuthentication ;
10
12
use Illuminate \Notifications \Notifiable ;
11
13
use Illuminate \Support \Facades \Log ;
12
14
use Laravel \Sanctum \HasApiTokens ;
13
15
use Silber \Bouncer \Database \HasRolesAndAbilities ;
16
+ use Spatie \Image \Manipulations ;
17
+ use Spatie \MediaLibrary \HasMedia ;
18
+ use Spatie \MediaLibrary \InteractsWithMedia ;
19
+ use Spatie \MediaLibrary \MediaCollections \Models \Media ;
14
20
15
- class User extends Authenticatable implements MustVerifyEmail
21
+ class User extends Authenticatable implements MustVerifyEmail, HasMedia
16
22
{
17
23
use HasApiTokens, HasFactory, Notifiable;
18
24
19
25
use HasRolesAndAbilities;
20
26
21
27
use Searchable, Filterable;
22
28
29
+ use InteractsWithMedia;
30
+
23
31
/**
24
32
* ALlowed search fields
25
33
* @var string[]
@@ -59,6 +67,7 @@ class User extends Authenticatable implements MustVerifyEmail
59
67
*/
60
68
protected $ appends = [
61
69
'avatar_url ' ,
70
+ 'avatar_thumb_url ' ,
62
71
'full_name ' ,
63
72
];
64
73
@@ -70,48 +79,41 @@ class User extends Authenticatable implements MustVerifyEmail
70
79
public static function boot ()
71
80
{
72
81
parent ::boot ();
73
-
74
- static ::deleting (function (self $ record ) {
75
- Log::info ('Deleting user... ' );
76
- Log::info (json_encode ($ record ->mediaFiles ));
77
- Log::info (json_encode ($ record ->mediaFiles ()->get ()));
78
- foreach ($ record ->mediaFiles ()->get () as $ entry ) {
79
- $ entry ->delete ();
80
- }
81
- });
82
82
}
83
83
84
84
/**
85
- * Returns the user avatar
86
- * @return \Illuminate\Database\Eloquent\Relations\HasOne|MediaFile
85
+ * @return \Closure|mixed|null|Media
87
86
*/
88
87
public function avatar ()
89
88
{
90
- return $ this ->hasOne (MediaFile::class, ' id ' , ' avatar_id ' );
89
+ return $ this ->getMedia ( ' avatars ' )-> first ( );
91
90
}
92
91
93
92
/**
94
- * Returns the user files
95
- * @return \Illuminate\Database\Eloquent\Relations\HasMany
93
+ * Returns the avatar url attribute
94
+ * @return string|null
96
95
*/
97
- public function mediaFiles ()
96
+ public function getAvatarUrlAttribute ()
98
97
{
99
- return $ this ->hasMany (MediaFile::class, 'user_id ' , 'id ' );
98
+ $ avatar = $ this ->avatar ();
99
+ if ($ avatar ) {
100
+ return $ avatar ->getFullUrl ();
101
+ }
102
+
103
+ return null ;
100
104
}
101
105
102
106
/**
103
107
* Returns the avatar url attribute
104
108
* @return string|null
105
109
*/
106
- public function getAvatarUrlAttribute ()
110
+ public function getAvatarThumbUrlAttribute ()
107
111
{
108
- $ src = $ this ->getAttribute ('avatar_id ' );
109
- if (is_null ($ src )) {
110
- return null ;
111
- }
112
- if (!empty ($ this ->avatar )) {
113
- return asset ('storage/ ' .$ this ->avatar ->path );
112
+ $ avatar = $ this ->avatar ();
113
+ if ($ avatar ) {
114
+ return $ avatar ->getAvailableFullUrl (['small_thumb ' ]);
114
115
}
116
+
115
117
return null ;
116
118
}
117
119
@@ -124,10 +126,11 @@ public function getFullNameAttribute()
124
126
$ names = [];
125
127
foreach (['first_name ' , 'middle_name ' , 'last_name ' ] as $ key ) {
126
128
$ value = $ this ->getAttribute ($ key );
127
- if (! empty ($ value )) {
129
+ if ( ! empty ($ value )) {
128
130
$ names [] = $ value ;
129
131
}
130
132
}
133
+
131
134
return implode (' ' , $ names );
132
135
}
133
136
@@ -139,4 +142,25 @@ public function getIsAdminAttribute()
139
142
{
140
143
return $ this ->isAn ('admin ' );
141
144
}
145
+
146
+ /**
147
+ * Register the conversions
148
+ *
149
+ * @param Media|null $media
150
+ *
151
+ * @return void
152
+ * @throws \Spatie\Image\Exceptions\InvalidManipulation
153
+ */
154
+ public function registerMediaConversions (Media $ media = null ): void
155
+ {
156
+ $ this ->addMediaConversion ('small_thumb ' )
157
+ ->fit (Manipulations::FIT_CROP , 300 , 300 )
158
+ ->nonQueued ();
159
+ $ this ->addMediaConversion ('medium_thumb ' )
160
+ ->fit (Manipulations::FIT_CROP , 600 , 600 )
161
+ ->nonQueued ();
162
+ $ this ->addMediaConversion ('large_thumb ' )
163
+ ->fit (Manipulations::FIT_CROP , 1200 , 1200 )
164
+ ->nonQueued ();
165
+ }
142
166
}
0 commit comments