diff --git a/config/comment.php b/config/comment.php index 0b6b128..4769f91 100644 --- a/config/comment.php +++ b/config/comment.php @@ -16,5 +16,5 @@ 'approved' => 1, 'rejected' => 2, 'spam' => 3, - ] -]; \ No newline at end of file + ], +]; diff --git a/database/migrations/create_comment_tables.php.stub b/database/migrations/create_comment_tables.php.stub index b91ff51..98a88e3 100644 --- a/database/migrations/create_comment_tables.php.stub +++ b/database/migrations/create_comment_tables.php.stub @@ -17,9 +17,9 @@ class CreateCommentTables extends Migration Schema::create($tableNames['comments'], function (Blueprint $table) { $table->id('id'); - $table->morphs('commenter'); + $table->uuidMorphs('commenter'); $table->unsignedBigInteger('parent_id')->nullable()->index(); - $table->morphs('commentable'); + $table->uuidMorphs('commentable'); $table->text('content'); $table->smallInteger('status')->default(0); $table->timestamps(); diff --git a/src/Events/CommentCreated.php b/src/Events/CommentCreated.php index ba8756a..85bb047 100644 --- a/src/Events/CommentCreated.php +++ b/src/Events/CommentCreated.php @@ -3,8 +3,8 @@ namespace BalajiDharma\LaravelComment\Events; use Illuminate\Broadcasting\InteractsWithSockets; -use Illuminate\Queue\SerializesModels; use Illuminate\Foundation\Events\Dispatchable; +use Illuminate\Queue\SerializesModels; class CommentCreated { @@ -21,4 +21,4 @@ public function __construct($comment) { $this->comment = $comment; } -} \ No newline at end of file +} diff --git a/src/Events/CommentDeleted.php b/src/Events/CommentDeleted.php index d5eefb1..2b0c710 100644 --- a/src/Events/CommentDeleted.php +++ b/src/Events/CommentDeleted.php @@ -3,8 +3,8 @@ namespace BalajiDharma\LaravelComment\Events; use Illuminate\Broadcasting\InteractsWithSockets; -use Illuminate\Queue\SerializesModels; use Illuminate\Foundation\Events\Dispatchable; +use Illuminate\Queue\SerializesModels; class CommentDeleted { @@ -21,4 +21,4 @@ public function __construct($comment) { $this->comment = $comment; } -} \ No newline at end of file +} diff --git a/src/Events/CommentUpdated.php b/src/Events/CommentUpdated.php index 61ab532..6d259c4 100644 --- a/src/Events/CommentUpdated.php +++ b/src/Events/CommentUpdated.php @@ -3,8 +3,8 @@ namespace BalajiDharma\LaravelComment\Events; use Illuminate\Broadcasting\InteractsWithSockets; -use Illuminate\Queue\SerializesModels; use Illuminate\Foundation\Events\Dispatchable; +use Illuminate\Queue\SerializesModels; class CommentUpdated { @@ -21,4 +21,4 @@ public function __construct($comment) { $this->comment = $comment; } -} \ No newline at end of file +} diff --git a/src/Models/Comment.php b/src/Models/Comment.php index 31816ea..894081f 100644 --- a/src/Models/Comment.php +++ b/src/Models/Comment.php @@ -42,7 +42,7 @@ class Comment extends Model * @var array */ protected $with = [ - 'commenter' + 'commenter', ]; /** @@ -80,5 +80,4 @@ public function parent() { return $this->belongsTo(config('comment.models.comment'), 'parent_id'); } - } diff --git a/src/Traits/HasCommenter.php b/src/Traits/HasCommenter.php index 3ac5874..2468986 100644 --- a/src/Traits/HasCommenter.php +++ b/src/Traits/HasCommenter.php @@ -15,7 +15,7 @@ trait HasCommenter */ public function comments() { - return $this->morphMany(Config::get('comment.models.comment'), 'commenter'); + return $this->morphMany(Config::get('comment.models.comment'), 'commenter', 'commenter_type', 'commenter_id', $this->getCommenterKey()); } /** @@ -23,7 +23,7 @@ public function comments() */ public function approvedComments() { - return $this->morphMany(Config::get('comment.models.comment'), 'commenter')->where('status', Config::get('comment.status.approved')); + return $this->morphMany(Config::get('comment.models.comment'), 'commenter', 'commenter_type', 'commenter_id', $this->getCommenterKey())->where('status', Config::get('comment.status.approved')); } /** @@ -31,7 +31,7 @@ public function approvedComments() */ public function pendingComments() { - return $this->morphMany(Config::get('comment.models.comment'), 'commenter')->where('status', Config::get('comment.status.pending')); + return $this->morphMany(Config::get('comment.models.comment'), 'commenter', 'commenter_type', 'commenter_id', $this->getCommenterKey())->where('status', Config::get('comment.status.pending')); } /** @@ -39,6 +39,16 @@ public function pendingComments() */ public function rejectedComments() { - return $this->morphMany(Config::get('comment.models.comment'), 'commenter')->where('status', Config::get('comment.status.rejected')); + return $this->morphMany(Config::get('comment.models.comment'), 'commenter', 'commenter_type', 'commenter_id', $this->getCommenterKey())->where('status', Config::get('comment.status.rejected')); } -} \ No newline at end of file + + public function getCommenterKey() + { + return $this->commenter_key ?? 'id'; + } + + public function setCommenterKey($key) + { + $this->commenter_key = $key; + } +} diff --git a/src/Traits/HasComments.php b/src/Traits/HasComments.php index 24135ab..698be2d 100644 --- a/src/Traits/HasComments.php +++ b/src/Traits/HasComments.php @@ -9,7 +9,7 @@ trait HasComments { public function comments(): MorphMany { - return $this->morphMany(Config::get('comment.models.comment'), 'commentable'); + return $this->morphMany(Config::get('comment.models.comment'), 'commentable', 'commentable_type', 'commentable_id', $this->getCommentableKey()); } /** @@ -17,7 +17,7 @@ public function comments(): MorphMany */ public function approvedComments() { - return $this->morphMany(Config::get('comment.models.comment'), 'commentable')->where('status', Config::get('comment.status.approved')); + return $this->morphMany(Config::get('comment.models.comment'), 'commentable', 'commentable_type', 'commentable_id', $this->getCommentableKey())->where('status', Config::get('comment.status.approved')); } /** @@ -25,7 +25,7 @@ public function approvedComments() */ public function pendingComments() { - return $this->morphMany(Config::get('comment.models.comment'), 'commentable')->where('status', Config::get('comment.status.pending')); + return $this->morphMany(Config::get('comment.models.comment'), 'commentable', 'commentable_type', 'commentable_id', $this->getCommentableKey())->where('status', Config::get('comment.status.pending')); } /** @@ -33,6 +33,16 @@ public function pendingComments() */ public function rejectedComments() { - return $this->morphMany(Config::get('comment.models.comment'), 'commentable')->where('status', Config::get('comment.status.rejected')); + return $this->morphMany(Config::get('comment.models.comment'), 'commentable', 'commentable_type', 'commentable_id', $this->getCommentableKey())->where('status', Config::get('comment.status.rejected')); } -} \ No newline at end of file + + public function getCommentableKey() + { + return $this->commentable_key ?? 'id'; + } + + public function setCommentableKey($key) + { + $this->commentable_key = $key; + } +}