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 88f4f8c commit 8f55c81
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
10 changes: 5 additions & 5 deletions app/Http/Controllers/TopicsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public function update(TopicRequest $request, Topic $topic)
}

public function destroy(Topic $topic)
{
$this->authorize('destroy', $topic);
$topic->delete();
{
$this->authorize('destroy', $topic);
$topic->delete();

return redirect()->route('topics.index')->with('message', 'Deleted successfully.');
}
return redirect()->route('topics.index')->with('success', '成功删除!');
}

public function uploadImage(Request $request, ImageUploadHandler $uploader)
{
Expand Down
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public function topics()
{
return $this->hasMany(Topic::class);
}

public function isAuthorOf($model)
{
return $this->id == $model->user_id;
}
}
4 changes: 2 additions & 2 deletions app/Policies/TopicPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class TopicPolicy extends Policy
{
public function update(User $user, Topic $topic)
{
return $topic->user_id == $user->id;
return $user->isAuthorOf($topic);
}

public function destroy(User $user, Topic $topic)
{
return true;
return $user->isAuthorOf($topic);
}
}
26 changes: 17 additions & 9 deletions resources/views/topics/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,23 @@
{!! $topic->body !!}
</div>

<div class="operate">
<hr>
<a href="{{ route('topics.edit', $topic->id) }}" class="btn btn-outline-secondary btn-sm" role="button">
<i class="far fa-edit"></i> 编辑
</a>
<a href="#" class="btn btn-outline-secondary btn-sm" role="button">
<i class="far fa-trash-alt"></i> 删除
</a>
</div>
@can('update', $topic)
<div class="operate">
<hr>
<a href="{{ route('topics.edit', $topic->id) }}" class="btn btn-outline-secondary btn-sm" role="button">
<i class="far fa-edit"></i> 编辑
</a>
<form action="{{ route('topics.destroy', $topic->id) }}" method="post"
style="display: inline-block;"
onsubmit="return confirm('您确定要删除吗?');">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-outline-secondary btn-sm">
<i class="far fa-trash-alt"></i> 删除
</button>
</form>
</div>
@endcan

</div>
</div>
Expand Down

0 comments on commit 8f55c81

Please sign in to comment.