Skip to content

Change user relation to morph #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/filament-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@

/*
* Authenticatable model class
*
* @deprecated
*/
'authenticatable' => \App\Models\User::class,

Expand Down
26 changes: 26 additions & 0 deletions database/migrations/change_user_relation_to_morph.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table(config('filament-comments.table_name', 'filament_comments'), function (Blueprint $table) {
$table->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');
});
}
};
1 change: 1 addition & 0 deletions src/FilamentCommentsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected function getMigrations(): array
return [
'create_filament_comments_table',
'add_index_to_subject',
'change_user_relation_to_morph',
];
}
}
1 change: 1 addition & 0 deletions src/Livewire/CommentsComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions src/Models/FilamentComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class FilamentComment extends Model
use SoftDeletes;

protected $fillable = [
'user_type',
'user_id',
'subject_type',
'subject_id',
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Policies/FilamentCommentPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down