diff --git a/app/Models/Topic.php b/app/Models/Topic.php index a3d6d3c..8be0669 100644 --- a/app/Models/Topic.php +++ b/app/Models/Topic.php @@ -7,7 +7,7 @@ class Topic extends Model protected $fillable = ['title', 'category_id', 'body', 'excerpt', 'slug']; - public function link($params) + public function link($params = []) { return route('topics.show', array_merge([$this->id, $this->slug], $params)); } diff --git a/app/Observers/ReplyObserver.php b/app/Observers/ReplyObserver.php index 61c35f1..ff43273 100644 --- a/app/Observers/ReplyObserver.php +++ b/app/Observers/ReplyObserver.php @@ -28,4 +28,9 @@ class ReplyObserver $topic->user->notify(new TopicReplied($reply)); } } + + public function deleted(Reply $reply) + { + $reply->topic->decrement('reply_count', 1); + } } \ No newline at end of file diff --git a/app/Observers/TopicObserver.php b/app/Observers/TopicObserver.php index b3fe8ad..6673de1 100644 --- a/app/Observers/TopicObserver.php +++ b/app/Observers/TopicObserver.php @@ -33,4 +33,9 @@ class TopicObserver { // } + + public function deleted(Topic $topic) + { + \DB::table('replies')->where('topic_id', $topic->id)->delete(); + } } \ No newline at end of file diff --git a/app/Policies/ReplyPolicy.php b/app/Policies/ReplyPolicy.php index cd54110..242e379 100644 --- a/app/Policies/ReplyPolicy.php +++ b/app/Policies/ReplyPolicy.php @@ -15,6 +15,6 @@ class ReplyPolicy extends Policy public function destroy(User $user, Reply $reply) { - return true; + return $user->isAuthorOf($reply) || $user->isAuthorOf($reply->topic); } } diff --git a/resources/views/topics/_reply_list.blade.php b/resources/views/topics/_reply_list.blade.php index f67ec7e..036e4b1 100644 --- a/resources/views/topics/_reply_list.blade.php +++ b/resources/views/topics/_reply_list.blade.php @@ -17,11 +17,17 @@
{{-- 回复删除按钮 --}} - + @can('destroy',$reply) + + @endcan