Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel passport动态范围_Php_Laravel - Fatal编程技术网

Php Laravel passport动态范围

Php Laravel passport动态范围,php,laravel,Php,Laravel,有没有办法设置laravel passport的动态范围而不是手动键入 我试图创建一个具有作用域名称和描述的表(MySql),创建了一个模型作用域,但该模型在AuthServiceProvider中不起作用 Passport::tokensCan([ 'view-dashboard' => 'View the contents of dashboard', 'view-reports' => 'View the detailed rep

有没有办法设置laravel passport的动态范围而不是手动键入

我试图创建一个具有作用域名称和描述的表(MySql),创建了一个模型作用域,但该模型在AuthServiceProvider中不起作用

Passport::tokensCan([
           'view-dashboard' => 'View the contents of dashboard',
           'view-reports'   => 'View the detailed reports'
       ]);
如何使作用域成为动态的?我想通过创建一些界面来管理基于用户角色的范围

编辑:我可以将解决方案与我目前的情况联系起来,这确实是有道理的,但你能帮我们解决问题吗

$token->scopes = user_scopes;
我在用户模型中尝试分配非对象的属性范围时遇到异常错误
,因为我已将您的代码添加到我的用户类中,如上所述。您能告诉我如何获取范围ID吗?

您可以像这样尝试:

  • 创建附加到用户的user\u scopes表,该表包含要分配给该用户的范围

  • 在用户类中重载withAccessToken()方法,这是HasApiTokens特性的一部分。使用此钩子从该用户的数据库中提取作用域,并将其分配给访问令牌:

  • 如果您想要充分的灵活性,请尝试扩展和重写src/Bridge/ScopeRepository类

    // in App\Providers\AppServiceProvider:
    
    use App\ScopeRepository;
    use Illuminate\Support\ServiceProvider;
    use Laravel\Passport\Bridge\ScopeRepository as PassportScopeRepository;
    
    class AppServiceProvider extends ServiceProvider
    {
        public function register()
        {
            $this->app->bind(PassportScopeRepository::class, ScopeRepository::class);
        }
    }
    
    

    请按此处查看完整的详细信息

    我已经更新了问题,你能帮我吗@VikashI想从数据库中获取作用域。上面的答案对我有什么作用?我已经浏览了您提供的github链接。我应该在哪里编写withAccessToken方法?我应该从哪里从db访问范围?@techguru
    $this->user\u scopes
    将自动从
    user\u scopes
    表中提取数据。。。如果您已定义了与
    用户
    用户范围的关系
    。。。明白了吗?是的,我现在明白了,谢谢。我会试着用AccessToken与您联系。
    withAccessToken
    从未执行过!我试图
    dd()
    但什么也没发生
    // in App\Providers\AppServiceProvider:
    
    use App\ScopeRepository;
    use Illuminate\Support\ServiceProvider;
    use Laravel\Passport\Bridge\ScopeRepository as PassportScopeRepository;
    
    class AppServiceProvider extends ServiceProvider
    {
        public function register()
        {
            $this->app->bind(PassportScopeRepository::class, ScopeRepository::class);
        }
    }