From dc18c87fcad673188f0ed61d2dd853e15d755c9a Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Wed, 31 Jan 2018 23:00:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=9B=9E=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Topic.php | 2 +- app/Observers/ReplyObserver.php | 5 +++++ app/Observers/TopicObserver.php | 5 +++++ app/Policies/ReplyPolicy.php | 2 +- resources/views/topics/_reply_list.blade.php | 16 +++++++++++----- 5 files changed, 23 insertions(+), 7 deletions(-) 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 @@ {{ $reply->created_at->diffForHumans() }} {{-- 回复删除按钮 --}} - - - - - + @can('destroy',$reply) + +
+ {{csrf_field()}} + {{method_field('DELETE')}} + +
+
+ @endcan
{!! $reply->content !!}