From 097a8feda48681e96fb0d42224a8ef37243fcafb Mon Sep 17 00:00:00 2001 From: Summer Date: Wed, 1 Nov 2017 07:27:11 +0000 Subject: [PATCH] User can delete reply --- app/Http/Controllers/RepliesController.php | 2 +- app/Observers/ReplyObserver.php | 5 +++++ app/Observers/TopicObserver.php | 5 +++++ app/Policies/ReplyPolicy.php | 10 ++-------- resources/views/topics/_reply_list.blade.php | 16 +++++++++++----- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/RepliesController.php b/app/Http/Controllers/RepliesController.php index bb6c1cf18..66f3355d5 100644 --- a/app/Http/Controllers/RepliesController.php +++ b/app/Http/Controllers/RepliesController.php @@ -30,6 +30,6 @@ public function destroy(Reply $reply) $this->authorize('destroy', $reply); $reply->delete(); - return redirect()->route('replies.index')->with('sucess', '回复删除成功!'); + return redirect()->to($reply->topic->link())->with('success', '成功删除回复!'); } } \ No newline at end of file diff --git a/app/Observers/ReplyObserver.php b/app/Observers/ReplyObserver.php index ec4d88da0..4f12a9e9a 100644 --- a/app/Observers/ReplyObserver.php +++ b/app/Observers/ReplyObserver.php @@ -25,4 +25,9 @@ public function creating(Reply $reply) { $reply->content = clean($reply->content, 'user_topic_body'); } + + 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 f969b5620..73fd40d5e 100644 --- a/app/Observers/TopicObserver.php +++ b/app/Observers/TopicObserver.php @@ -28,4 +28,9 @@ public function saved(Topic $topic) dispatch(new TranslateSlug($topic)); } } + + 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 a5b6096b0..89cc08fb2 100644 --- a/app/Policies/ReplyPolicy.php +++ b/app/Policies/ReplyPolicy.php @@ -7,14 +7,8 @@ class ReplyPolicy extends Policy { - public function update(User $user, Reply $reply) - { - // return $reply->user_id == $user->id; - return true; - } - public function destroy(User $user, Reply $reply) { - return true; + return $user->isAuthorOf($reply) || $user->isAuthorOf($reply->topic); } -} +} \ No newline at end of file diff --git a/resources/views/topics/_reply_list.blade.php b/resources/views/topics/_reply_list.blade.php index d8882810c..571c4b142 100644 --- a/resources/views/topics/_reply_list.blade.php +++ b/resources/views/topics/_reply_list.blade.php @@ -16,11 +16,17 @@ {{ $reply->created_at->diffForHumans() }} {{-- 回复删除按钮 --}} - - - - - + @can('destroy', $reply) + +
+ {{ csrf_field() }} + {{ method_field('DELETE') }} + +
+
+ @endcan
{!! $reply->content !!}