Skip to content

Commit

Permalink
Updated commentable and commenter type
Browse files Browse the repository at this point in the history
  • Loading branch information
balajidharma committed Dec 8, 2024
1 parent 9d27943 commit 43f842f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 22 deletions.
4 changes: 2 additions & 2 deletions config/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
'approved' => 1,
'rejected' => 2,
'spam' => 3,
]
];
],
];
4 changes: 2 additions & 2 deletions database/migrations/create_comment_tables.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/Events/CommentCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -21,4 +21,4 @@ public function __construct($comment)
{
$this->comment = $comment;
}
}
}
4 changes: 2 additions & 2 deletions src/Events/CommentDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -21,4 +21,4 @@ public function __construct($comment)
{
$this->comment = $comment;
}
}
}
4 changes: 2 additions & 2 deletions src/Events/CommentUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -21,4 +21,4 @@ public function __construct($comment)
{
$this->comment = $comment;
}
}
}
3 changes: 1 addition & 2 deletions src/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Comment extends Model
* @var array
*/
protected $with = [
'commenter'
'commenter',
];

/**
Expand Down Expand Up @@ -80,5 +80,4 @@ public function parent()
{
return $this->belongsTo(config('comment.models.comment'), 'parent_id');
}

}
20 changes: 15 additions & 5 deletions src/Traits/HasCommenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,40 @@ 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());
}

/**
* Returns only approved comments that this user has made.
*/
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'));
}

/**
* Returns only pending comments that this user has made.
*/
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'));
}

/**
* Returns only rejected comments that this user has made.
*/
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'));
}
}

public function getCommenterKey()
{
return $this->commenter_key ?? 'id';
}

public function setCommenterKey($key)
{
$this->commenter_key = $key;
}
}
20 changes: 15 additions & 5 deletions src/Traits/HasComments.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,40 @@ 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());
}

/**
* Returns only approved comments that this user has made.
*/
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'));
}

/**
* Returns only pending comments that this user has made.
*/
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'));
}

/**
* Returns only rejected comments that this user has made.
*/
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'));
}
}

public function getCommentableKey()
{
return $this->commentable_key ?? 'id';
}

public function setCommentableKey($key)
{
$this->commentable_key = $key;
}
}

0 comments on commit 43f842f

Please sign in to comment.