Skip to content

Commit

Permalink
删除评论
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Mar 6, 2022
1 parent a026a74 commit 8ba6cfc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/RepliesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function destroy(Reply $reply)
$this->authorize('destroy', $reply);
$reply->delete();

return redirect()->route('replies.index')->with('success', '评论删除成功!');
return redirect()->to($reply->topic->link())->with('success', '评论删除成功!');
}
}
6 changes: 6 additions & 0 deletions app/Models/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ public function link($params = [])
{
return route('topics.show', array_merge([$this->id, $this->slug], $params));
}

public function updateReplyCount()
{
$this->reply_count = $this->replies->count();
$this->save();
}
}
12 changes: 8 additions & 4 deletions app/Observers/ReplyObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
namespace App\Observers;

use App\Models\Reply;
use App\Notifications\TopicReplied;

// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored

use App\Notifications\TopicReplied;

class ReplyObserver
{
public function created(Reply $reply)
{
$reply->topic->reply_count = $reply->topic->replies->count();
$reply->topic->save();

$reply->topic->updateReplyCount();
// 通知话题作者有新的评论
$reply->topic->user->notify(new TopicReplied($reply));
}
Expand All @@ -23,4 +22,9 @@ public function creating(Reply $reply)
{
$reply->content = clean($reply->content, 'user_topic_body');
}

public function deleted(Reply $reply)
{
$reply->topic->updateReplyCount();
}
}
6 changes: 6 additions & 0 deletions app/Observers/TopicObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Topic;
use App\Jobs\TranslateSlug;
use Illuminate\Support\Facades\DB;

// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored
Expand All @@ -28,4 +29,9 @@ public function saved(Topic $topic)
dispatch(new TranslateSlug($topic));
}
}

public function deleted(Topic $topic)
{
DB::table('replies')->where('topic_id', $topic->id)->delete();
}
}
8 changes: 1 addition & 7 deletions app/Policies/ReplyPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
19 changes: 14 additions & 5 deletions resources/views/topics/_reply_list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@
<span class="meta text-secondary" title="{{ $reply->created_at }}">{{ $reply->created_at->diffForHumans() }}</span>

{{-- 回复删除按钮 --}}
<span class="meta float-end ">
<a title="删除回复">
<i class="far fa-trash-alt"></i>
</a>
</span>
@can('destroy', $reply)
<span class="meta float-end">
<form action="{{ route('replies.destroy', $reply->id) }}"
onsubmit="return confirm('确定要删除此评论?');"
method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-default btn-xs pull-left text-secondary">
<i class="far fa-trash-alt"></i>
</button>
</form>
</span>
@endcan

</div>
<div class="reply-content text-secondary">
{!! $reply->content !!}
Expand Down

0 comments on commit 8ba6cfc

Please sign in to comment.