1313use Spatie \MediaLibrary \InteractsWithMedia ;
1414use Spatie \MediaLibrary \MediaCollections \Models \Media ;
1515
16+ /**
17+ * @property int $id
18+ * @property string $name
19+ * @property string $slug
20+ * @property string $type
21+ * @property int|null $parent_id
22+ * @property int $created_by
23+ * @property int|null $updated_by
24+ * @property string|null $external_url
25+ * @property string|null $link_description
26+ * @property string|null $general_access
27+ * @property \Illuminate\Support\Carbon|null $created_at
28+ * @property \Illuminate\Support\Carbon|null $updated_at
29+ * @property \Illuminate\Support\Carbon|null $deleted_at
30+ * @property-read LibraryItem|null $parent
31+ * @property-read \Illuminate\Database\Eloquent\Collection<int, LibraryItem> $children
32+ * @property-read \Illuminate\Database\Eloquent\Model $creator
33+ * @property-read \Illuminate\Database\Eloquent\Model|null $updater
34+ * @property-read \Illuminate\Database\Eloquent\Collection<int, LibraryItemPermission> $permissions
35+ * @property-read \Illuminate\Database\Eloquent\Collection<int, LibraryItemTag> $tags
36+ */
1637class LibraryItem extends Model implements HasMedia
1738{
1839 use HasFactory;
@@ -58,14 +79,16 @@ protected static function boot(): void
5879
5980 static ::created (function (self $ item ) {
6081 // Copy parent folder permissions to the new item
61- if ($ item ->parent_id ) {
82+ if ($ item ->parent_id && $ item -> parent ) {
6283 $ parentPermissions = $ item ->parent ->permissions ()->get ();
6384
6485 foreach ($ parentPermissions as $ permission ) {
65- $ item ->permissions ()->create ([
66- 'user_id ' => $ permission ->user_id ,
67- 'role ' => $ permission ->role ,
68- ]);
86+ if (isset ($ permission ->user_id ) && isset ($ permission ->role )) {
87+ $ item ->permissions ()->create ([
88+ 'user_id ' => $ permission ->user_id ,
89+ 'role ' => $ permission ->role ,
90+ ]);
91+ }
6992 }
7093 }
7194 });
@@ -103,7 +126,9 @@ public function children(): HasMany
103126 */
104127 public function creator (): BelongsTo
105128 {
106- return $ this ->belongsTo (\App \Models \User::class, 'created_by ' )->withDefault (function () {
129+ $ userModel = config ('filament-library.user_model ' , config ('auth.providers.users.model ' , 'App \\Models \\User ' ));
130+
131+ return $ this ->belongsTo ($ userModel , 'created_by ' )->withDefault (function () {
107132 // Check if 'name' field exists
108133 if (\Illuminate \Support \Facades \Schema::hasColumn ('users ' , 'name ' )) {
109134 return [
@@ -126,7 +151,9 @@ public function creator(): BelongsTo
126151 */
127152 public function updater (): BelongsTo
128153 {
129- return $ this ->belongsTo (\App \Models \User::class, 'updated_by ' );
154+ $ userModel = config ('filament-library.user_model ' , config ('auth.providers.users.model ' , 'App \\Models \\User ' ));
155+
156+ return $ this ->belongsTo ($ userModel , 'updated_by ' );
130157 }
131158
132159 /**
@@ -199,7 +226,7 @@ public function getEffectiveRole($user): ?string
199226 ->where ('user_id ' , $ user ->id )
200227 ->first ();
201228
202- if ($ directPermission ) {
229+ if ($ directPermission && isset ( $ directPermission -> role ) ) {
203230 return $ directPermission ->role ;
204231 }
205232
@@ -221,13 +248,13 @@ public function getEffectiveRole($user): ?string
221248 /**
222249 * Get the current owner of this item.
223250 */
224- public function getCurrentOwner (): ?\App \ Models \ User
251+ public function getCurrentOwner (): ?\Illuminate \ Database \ Eloquent \ Model
225252 {
226253 $ ownerPermission = $ this ->permissions ()
227254 ->where ('role ' , 'owner ' )
228255 ->first ();
229256
230- if ($ ownerPermission ) {
257+ if ($ ownerPermission && $ ownerPermission -> user ) {
231258 return $ ownerPermission ->user ;
232259 }
233260
@@ -248,7 +275,7 @@ public function isCreatorOwner(): bool
248275 /**
249276 * Transfer ownership to another user.
250277 */
251- public function transferOwnership (\App \ Models \ User $ newOwner ): void
278+ public function transferOwnership (\Illuminate \ Database \ Eloquent \ Model $ newOwner ): void
252279 {
253280 // Remove existing owner permissions
254281 $ this ->permissions ()->where ('role ' , 'owner ' )->delete ();
@@ -271,7 +298,7 @@ public function transferOwnership(\App\Models\User $newOwner): void
271298 /**
272299 * Ensure a user has a personal folder (like Google Drive's "My Drive").
273300 */
274- public static function ensurePersonalFolder (\App \ Models \ User $ user ): self
301+ public static function ensurePersonalFolder (\Illuminate \ Database \ Eloquent \ Model $ user ): self
275302 {
276303 // Check if user already has a personal folder via the relationship
277304 if ($ user ->personal_folder_id ) {
@@ -306,7 +333,7 @@ public static function ensurePersonalFolder(\App\Models\User $user): self
306333 /**
307334 * Get a user's personal folder.
308335 */
309- public static function getPersonalFolder (\App \ Models \ User $ user ): ?self
336+ public static function getPersonalFolder (\Illuminate \ Database \ Eloquent \ Model $ user ): ?self
310337 {
311338 if (! $ user ->personal_folder_id ) {
312339 return null ;
@@ -318,7 +345,7 @@ public static function getPersonalFolder(\App\Models\User $user): ?self
318345 /**
319346 * Generate the personal folder name for a user.
320347 */
321- public static function getPersonalFolderName (\App \ Models \ User $ user ): string
348+ public static function getPersonalFolderName (\Illuminate \ Database \ Eloquent \ Model $ user ): string
322349 {
323350 // Try to get a display name from various user fields
324351 $ name = $ user ->first_name ?? $ user ->name ?? $ user ->email ?? 'User ' ;
0 commit comments