mirror of
https://github.com/wearepixel/laravel-comments.git
synced 2026-11-14 14:02:04 +00:00
Allow deleting replies when comments are deleted.
This commit is contained in:
+23
-3
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace BeyondCode\Comments;
|
||||
|
||||
use BeyondCode\Comments\Events\CommentAdded;
|
||||
use BeyondCode\Comments\Events\CommentDeleted;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use BeyondCode\Comments\Traits\HasComments;
|
||||
@@ -20,6 +22,25 @@ class Comment extends Model
|
||||
'is_approved' => 'boolean'
|
||||
];
|
||||
|
||||
public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::deleting(function (self $model) {
|
||||
if (config('comments.delete_replies_along_comments')) {
|
||||
$model->comments()->delete();
|
||||
}
|
||||
});
|
||||
|
||||
static::deleted(function (self $model) {
|
||||
CommentDeleted::dispatch($model);
|
||||
});
|
||||
|
||||
static::created(function (self $model) {
|
||||
CommentAdded::dispatch($model);
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeApproved($query)
|
||||
{
|
||||
return $query->where('is_approved', true);
|
||||
@@ -43,7 +64,7 @@ class Comment extends Model
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function disapprove()
|
||||
{
|
||||
$this->update([
|
||||
@@ -65,5 +86,4 @@ class Comment extends Model
|
||||
|
||||
throw new Exception('Could not determine the commentator model name.');
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\Comments\Events;
|
||||
|
||||
use BeyondCode\Comments\Comment;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class CommentAdded implements ShouldDispatchAfterCommit
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*/
|
||||
public function __construct(
|
||||
public Comment $comment,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\Comments\Events;
|
||||
|
||||
use BeyondCode\Comments\Comment;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class CommentDeleted implements ShouldDispatchAfterCommit
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*/
|
||||
public function __construct(
|
||||
public Comment $comment,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ trait HasComments
|
||||
|
||||
$comment = new $commentClass([
|
||||
'comment' => $comment,
|
||||
'is_approved' => ($user instanceof Commentator) ? ! $user->needsCommentApproval($this) : false,
|
||||
'is_approved' => ($user instanceof Commentator) ? !$user->needsCommentApproval($this) : false,
|
||||
'user_id' => is_null($user) ? null : $user->getKey(),
|
||||
'commentable_id' => $this->getKey(),
|
||||
'commentable_type' => get_class(),
|
||||
@@ -51,5 +51,4 @@ trait HasComments
|
||||
|
||||
return $this->comments()->save($comment);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user