Skip to content

Commit

Permalink
User can delete reply
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Nov 1, 2017
1 parent 943e1ff commit 097a8fe
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 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('sucess', '回复删除成功');
return redirect()->to($reply->topic->link())->with('success', '成功删除回复');
}
}
5 changes: 5 additions & 0 deletions app/Observers/ReplyObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions app/Observers/TopicObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
10 changes: 2 additions & 8 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);
}
}
}
16 changes: 11 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,17 @@
<span class="meta" title="{{ $reply->created_at }}">{{ $reply->created_at->diffForHumans() }}</span>

{{-- 回复删除按钮 --}}
<span class="meta pull-right">
<a title="删除回复">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</a>
</span>
@can('destroy', $reply)
<span class="meta pull-right">
<form action="{{ route('replies.destroy', $reply->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-default btn-xs pull-left">
<i class="glyphicon glyphicon-trash"></i>
</button>
</form>
</span>
@endcan
</div>
<div class="reply-content">
{!! $reply->content !!}
Expand Down

0 comments on commit 097a8fe

Please sign in to comment.