Skip to content

Commit 5246ad6

Browse files
committed
User model from config and fix relationship issue
1 parent 7d369a5 commit 5246ad6

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

src/Models/LibraryItem.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,17 @@ public function children(): HasMany
105105
*/
106106
public function creator(): BelongsTo
107107
{
108-
return $this->belongsTo(\App\Models\User::class, 'created_by')->withDefault(function () {
108+
return $this->belongsTo(config('filament-library.user_model'), 'created_by')->withDefault(function ($instance) {
109109
// Check if 'name' field exists
110110
if (\Illuminate\Support\Facades\Schema::hasColumn('users', 'name')) {
111-
return [
112-
'name' => 'Unknown User',
113-
'email' => '[email protected]',
114-
];
111+
$instance->name = 'Unknown User';
112+
$instance->email = '[email protected]';
113+
} else {
114+
// Fall back to first_name/last_name
115+
$instance->first_name = 'Unknown';
116+
$instance->last_name = 'User';
117+
$instance->email = '[email protected]';
115118
}
116-
117-
// Fall back to first_name/last_name
118-
return [
119-
'first_name' => 'Unknown',
120-
'last_name' => 'User',
121-
'email' => '[email protected]',
122-
];
123119
});
124120
}
125121

@@ -128,7 +124,7 @@ public function creator(): BelongsTo
128124
*/
129125
public function updater(): BelongsTo
130126
{
131-
return $this->belongsTo(\App\Models\User::class, 'updated_by');
127+
return $this->belongsTo(config('filament-library.user_model'), 'updated_by');
132128
}
133129

134130
/**
@@ -223,7 +219,7 @@ public function getEffectiveRole($user): ?string
223219
/**
224220
* Get the current owner of this item.
225221
*/
226-
public function getCurrentOwner(): ?\App\Models\User
222+
public function getCurrentOwner(): ?Model
227223
{
228224
$ownerPermission = $this->permissions()
229225
->where('role', 'owner')
@@ -250,7 +246,7 @@ public function isCreatorOwner(): bool
250246
/**
251247
* Transfer ownership to another user.
252248
*/
253-
public function transferOwnership(\App\Models\User $newOwner): void
249+
public function transferOwnership(Model $newOwner): void
254250
{
255251
// Remove existing owner permissions
256252
$this->permissions()->where('role', 'owner')->delete();
@@ -273,7 +269,7 @@ public function transferOwnership(\App\Models\User $newOwner): void
273269
/**
274270
* Ensure a user has a personal folder (like Google Drive's "My Drive").
275271
*/
276-
public static function ensurePersonalFolder(\App\Models\User $user): self
272+
public static function ensurePersonalFolder(Model $user): self
277273
{
278274
// Check if user already has a personal folder via the relationship
279275
if ($user->personal_folder_id) {
@@ -308,7 +304,7 @@ public static function ensurePersonalFolder(\App\Models\User $user): self
308304
/**
309305
* Get a user's personal folder.
310306
*/
311-
public static function getPersonalFolder(\App\Models\User $user): ?self
307+
public static function getPersonalFolder(Model $user): ?self
312308
{
313309
if (! $user->personal_folder_id) {
314310
return null;
@@ -320,7 +316,7 @@ public static function getPersonalFolder(\App\Models\User $user): ?self
320316
/**
321317
* Generate the personal folder name for a user.
322318
*/
323-
public static function getPersonalFolderName(\App\Models\User $user): string
319+
public static function getPersonalFolderName(Model $user): string
324320
{
325321
// Try to get a display name from various user fields
326322
$name = $user->first_name ?? $user->name ?? $user->email ?? 'User';

src/Models/LibraryItemPermission.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public function libraryItem(): BelongsTo
3838
*/
3939
public function user(): BelongsTo
4040
{
41-
$userModel = config('auth.providers.users.model', 'App\\Models\\User');
42-
43-
return $this->belongsTo($userModel);
41+
return $this->belongsTo(config('filament-library.user_model'));
4442
}
4543

4644
/**

0 commit comments

Comments
 (0)