diff --git a/config/filament-comments.php b/config/filament-comments.php index 286b622..8c89e18 100644 --- a/config/filament-comments.php +++ b/config/filament-comments.php @@ -63,6 +63,8 @@ /* * Authenticatable model class + * + * @deprecated */ 'authenticatable' => \App\Models\User::class, diff --git a/database/migrations/change_user_relation_to_morph.php.stub b/database/migrations/change_user_relation_to_morph.php.stub new file mode 100644 index 0000000..7f683e9 --- /dev/null +++ b/database/migrations/change_user_relation_to_morph.php.stub @@ -0,0 +1,26 @@ +string('user_type')->after('id')->default(str_replace('\\', '\\\\', config('filament-comments.authenticatable', 'filament_comments'))); + + $table->index(['user_type', 'user_id']); + }); + } + + public function down(): void + { + Schema::table(config('filament-comments.table_name', 'filament_comments'), function (Blueprint $table) { + $table->dropIndex(['user_type', 'user_id']); + + $table->dropColumn('user_type'); + }); + } +}; diff --git a/src/FilamentCommentsServiceProvider.php b/src/FilamentCommentsServiceProvider.php index a90b6d5..b69696f 100644 --- a/src/FilamentCommentsServiceProvider.php +++ b/src/FilamentCommentsServiceProvider.php @@ -86,6 +86,7 @@ protected function getMigrations(): array return [ 'create_filament_comments_table', 'add_index_to_subject', + 'change_user_relation_to_morph', ]; } } diff --git a/src/Livewire/CommentsComponent.php b/src/Livewire/CommentsComponent.php index 021999d..bd9b65e 100644 --- a/src/Livewire/CommentsComponent.php +++ b/src/Livewire/CommentsComponent.php @@ -67,6 +67,7 @@ public function create(): void 'subject_type' => $this->record->getMorphClass(), 'comment' => $data['comment'], 'user_id' => auth()->id(), + 'user_type' => auth()->user()->getMorphClass(), ]); Notification::make() diff --git a/src/Models/FilamentComment.php b/src/Models/FilamentComment.php index a75b7bf..3544413 100644 --- a/src/Models/FilamentComment.php +++ b/src/Models/FilamentComment.php @@ -15,6 +15,7 @@ class FilamentComment extends Model use SoftDeletes; protected $fillable = [ + 'user_type', 'user_id', 'subject_type', 'subject_id', @@ -34,9 +35,7 @@ public function __construct(array $attributes = []) public function user(): BelongsTo { - $authenticatable = config('filament-comments.authenticatable'); - - return $this->belongsTo($authenticatable, 'user_id'); + return $this->morphTo(); } public function subject(): BelongsTo diff --git a/src/Policies/FilamentCommentPolicy.php b/src/Policies/FilamentCommentPolicy.php index 6cea8eb..b844a4c 100644 --- a/src/Policies/FilamentCommentPolicy.php +++ b/src/Policies/FilamentCommentPolicy.php @@ -29,7 +29,7 @@ public function update(Authenticatable $user, FilamentComment $filamentComment): public function delete(Authenticatable $user, FilamentComment $filamentComment): bool { - return $user->id === $filamentComment->user_id; + return $user->id === $filamentComment->user_id && $user->getMorphClass() === $filamentComment->user_type; } public function deleteAny(Authenticatable $user): bool