Laravel 帖子和评论关系,只有帖子作者可以删除评论。但发表评论的用户也可以编辑和删除

问题描述 投票:0回答:1

我不知道该怎么做。如果有人知道的话。让我知道。我认为这与验证有关。请指导我。

只有帖子作者可以删除评论。但发表评论的用户也可以编辑和删除

php laravel laravel-5 relational
1个回答
0
投票

您可以通过简单的方式检查经过身份验证的用户并启用禁用操作按钮

在您的控制器中

   public function editComment(Comment $comment)
{
    // Determine if the user is the author of the comment
    $userIsCommentAuthor = Auth::id() === $comment->user_id;

    return view('comment.edit', compact('comment', 'userIsCommentAuthor'));
}
  public function deleteComment(Comment $comment)
{
    // Determine if the user is the author of the post
    $userIsAuthor = Auth::id() === $comment->post->user_id;

    // Determine if the user is the author of the comment
    $userIsCommentAuthor = Auth::id() === $comment->user_id;

    return view('comment.delete', compact('comment', 'userIsAuthor', 'userIsCommentAuthor'));
}

在您的刀片文件中

<!-- Display comments -->
@foreach ($comments as $comment)
    <!-- Display comment content -->

    @if ($userIsAuthor)
        <!-- Display delete button for the comment -->
        <a href="{{ route('comment.delete', $comment->id) }}">Delete Comment</a>
    @endif

    @if ($userIsCommentAuthor)
        <!-- Display edit button for the comment -->
        <a href="{{ route('comment.edit', $comment->id) }}">Edit Comment</a>
    @endif
@endforeach
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.