You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel评论权限政策失效修复求助(Inertia JS环境)

问题:Inertia JS论坛评论权限政策失效修复求助

我用Inertia JS搭建了一个支持帖子和评论CRUD操作的论坛,设定仅内容创作者及管理员可修改/删除对应内容。目前帖子权限政策已正常生效,但评论权限政策无法工作,请求协助修复。


相关代码

帖子与评论的show方法

public function show(Post $post, Comment $comment)
{
    usleep(500000);

    $post->incrementReadCount();

    $updateableCommentIds = $post->comments
        ->map(function ($comment) {
            if (Auth::user()->can('update', $comment)) {
                return $comment->id;
            }
        })
        ->filter();

    return Inertia::render('Frontend/Forum/Helpers/PostDetails', [
        'post' => PostResource::make(
            $post->load('user')->loadCount('comments')
        ),
        'comments' => CommentResource::collection(
            Comment::where('post_id', $post->id)
                ->with('user')
                ->paginate(10)
                ->withQueryString()
        ),
        'categories' => Category::all(),
        'can' => [
            'edit' => Auth::check() && Auth::user()->can('edit', $post),
            'commentEdit' => $updateableCommentIds
        ]
    ]);
}

评论权限政策

class CommentPolicy
{
    use HandlesAuthorization;

    public function update(User $user, Comment $comment): bool
    {
        return $user->is_admin || $user->id === (int) $comment->user_id;
    }
}

Vue文件相关代码

<div
v-if="can.commentEdit.includes(comment.id)"
>
        // 仅评论作者可见的操作区域
</div>

已尝试的修改(无效)

public function show(Post $post)
{
    $canUpdateComments = $post->comments->every(function ($comment) {
        return Auth::user()->can('update', $comment);
    });

    // 返回视图并传递评论修改权限
    return view('posts.show', compact('post', 'canUpdateComments'));
}

内容的提问来源于stack exchange,提问作者dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 09:45:28