【问题标题】:define a policy for Comments deleting that checks comment author or post author (laravel)定义评论删除策略,检查评论作者或帖子作者(laravel)
【发布时间】:2016-11-25 05:14:12
【问题描述】:

如何让帖子的作者和评论的作者都删除它? 我的意思是,帖子的作者可以删除他帖子下的所有评论,而评论的作者只能删除他留下的cmets。

到目前为止,我有一个这样工作的 commentPolicy 类:

class CommentPolicy
{
    public function deleteComment(User $user, Comment $comment)
    {

        return $user->id === $comment->user_id;

    }

}

当然,这适用于评论的作者。我在想的是我应该让帖子的作者删除 postPolicy 类中的 cmets。我有的是:

class PostPolicy

{

    public function seePostOptions(User $user, Post $post)
    {
        return $user->id === $post->user_id;
    }


}

现在帖子的作者可以看到一些选项来编辑/删除/评论帖子。 我在想也许我应该只放两个 @can('deleteComment', $c)<>delete button<>@endcan@can('seePostOptions' $i)<>delete button<>@endcan 但这会导致帖子作者在他的帖子下留下的所有那些 cmets 的双按钮。

【问题讨论】:

    标签: laravel comments acl posts policy


    【解决方案1】:

    解决方案是像这样使用@else 指令:

    @can('deleteComment', $c)
    <p>delete</p>
    @else
    @can('seePostOptions', $i)
    <p>delete</p>
    @endcan
    @endcan
    

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多