LoginSignup
6
5

More than 5 years have passed since last update.

Laravel デフォルトの User クラス以外で Gate, Policy (認可)を使うために必要なこと

Last updated at Posted at 2018-05-21

確認環境

  • Laravel 5.6
  • PHP 7.1

問題

何もせずに使うと次のようなエラーに遭遇する。

Type error: Argument 1 passed to Barryvdh\Debugbar\DataCollector\GateCollector::addCheck() must be an instance of Illuminate\Contracts\Auth\Access\Authorizable or null, instance of <独自クラス名> in <アプリケーションへのパス>/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 374 

解決策

認可の仕組みである Gate, Policy を使うためには Illuminate\Contracts\Auth\Access\Authorizable
という interface を実装している必要がある。

この実装は Illuminate\Foundation\Auth\Access\Authorizable という trait が存在するので、これを use してあげればよい。

<?php

// Authorizable という名前が被っているので AuthorizableContract とする
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Foundation\Auth\Access\Authorizable;

class User implements AuthorizableContract
{
    use Authorizable;

    // ...以下略
}

参考

6
5
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
6
5