【问题标题】:Delete Post Authorize Laravel API删除授权后 Laravel API
【发布时间】:2021-11-28 20:25:26
【问题描述】:

我想删除具有授权的帖子,但失败并出现错误“消息”:false, "errors": "此操作未经授权。"

销毁控制器

public function destroy($id, Post $post)
{
    try {
        $this->authorize('delete', $post);
        $posts = Post::find($id);
        $posts->delete();
        return response()->json([
            'success' => true,
            'message' => 'Success'
        ]);
    } catch (\Exception $e) {
        return response()->json([
            'message' => false,
            'errors' => $e->getMessage()
        ]);
    }

}

政策

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

Postman

【问题讨论】:

    标签: php laravel api authorization policy


    【解决方案1】:

    确保用户连接良好。你用的是哪个api。?如果是圣所或护照,您需要在 Postman 的 Header Request 中指定,属性 Authorization 与值 Bearer your_token 这将创建一个像您一样的请求,作为拥有令牌的用户连接。 你也可以通过像这样调试来检查值,然后查看响应。

    var_dump($post->user_id);
    var_dump($user);
    die();
    

    【讨论】:

    • NULL 我应该添加什么?因为我已经使用令牌
    • 我用 sanctum 做 api
    • 按照这里的所有步骤:laravel.com/docs/8.x/sanctum
    猜你喜欢
    • 2018-10-17
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2020-07-04
    相关资源
    最近更新 更多