permission 在模型中配置了 $guard_name,但並未起效。求大佬指點。

carveybunt發表於2019-12-19
  • 模型程式碼

    <?php
    
    namespace App\Http\Models\Api\Server;
    
    use Auth;
    use Illuminate\Database\Eloquent\SoftDeletes;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Spatie\Permission\Models\Permission;
    use Spatie\Permission\Traits\HasRoles;
    use Tymon\JWTAuth\Contracts\JWTSubject;
    
    class Servers extends Authenticatable implements JWTSubject
    {
        use HasRoles;
        protected $guard_name = 'server';
        public function __construct()
        {
            if (!cache()->has('hasPermission_server_manage_user')) {
                if (Permission::findOrCreate(0,'server_manage_user', '管理使用者')) {
                    cache()->set('hasPermission_server_manage_user', 1, 60 * 60 * 24);
                }
            }
        }
        public function getJWTIdentifier()
        {
            return $this->getKey();
        }

    -執行後資料庫中

微信圖片_20191219100327

  • config/auth.php

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
    'web' => [
    'driver' => 'session',
    'provider' => 'users',
    ],

    'user' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
    'server' => [
        'driver' => 'jwt',
        'provider' => 'servers',
    ],

    ],

有大佬能指點下嗎?

相關文章