1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Laravel】ポリシーのauthorizeメソッドの引数についてのメモ

Last updated at Posted at 2022-04-22

環境 Laravel6.2

ポリシー側で使わないモデルでも引数に入れる必要がある

例えば、ArticleクラスからArticlePolicyを使用したくてauthorizeメソッドを使う際、
Policy側のメソッド内で特にArticleモデルが必要なかったとしても
どのポリシーを使うのかは指定しなければならないので
$this->authorize('ポリシー側のメソッド名', Article::class);
といった形でArticleモデルを引数に入れる必要がある。

モデル以外で渡したい値がある場合

下記のように配列で渡す。

public function deleteComment(Request $request)
    {
        $comment = Comment::find($request->commeid);
        $this->authorize('deleteComment', [Article::class,$comment->user_id]);
        $comment->delete();
    }

ちなみにArticlePolicy側は下記のような感じ。

public function deleteComment(User $user,int $commentUserId)
    {
        return $user->id === $commentUserId;
    }

公式

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?