【发布时间】: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;
}
【问题讨论】:
标签: php laravel api authorization policy