Vue.js with Laravel Permission

后端 未结 5 645
旧时难觅i
旧时难觅i 2020-12-31 22:38

I am in the process of integrating Laravel Permission API with Vue.JS frontend. I am using https://github.com/spatie/laravel-permission library for Laravel Permission. I am

5条回答
  •  长情又很酷
    2020-12-31 23:01

    You can use this format in Vuejs for Laravel Permission:

    add function to User Model to get all user permissions&roles like this:

    class User extends Authenticatable
    {
        // ...
    
        public function jsPermissions()
        {
            return json_encode([
                    'roles' => $this->getRoleNames(),
                    'permissions' => $this->getAllPermissions()->pluck('name'),
                ]);
        }
    }
    

    pass this data to JavaScript in HTML header:

    
    

    in app.js file add global Vuejs can function to check user permissions and is function to check user roles:

    Vue.prototype.can = function(value){
        return window.Laravel.jsPermissions.permissions.includes(value);
    }
    Vue.prototype.is = function(value){
        return window.Laravel.jsPermissions.roles.includes(value);
    }
    

    https://github.com/ahmedsaoud31/laravel-permission-to-vuejs

提交回复
热议问题