首页 > 解决方案 > WordPress 角色和功能的雄辩模型

问题描述

我正在使用 Laravel 访问一些 WordPress 表,我的问题是我不了解 WordPress 角色和功能是如何工作的,所以有人可以给我一个 Schema 或解释我如何将角色和功能转换为雄辩的模型吗?

我做了什么:

    class User extend Model {
    /**
         * Get the User meta.
         *
         * @return \Illuminate\Database\Eloquent\Relations\Relation
         */
        public function meta(): Relation
        {
            return $this->hasMany(UserMeta::class, 'user_id');
        }
        
    /**
    * Get the meta value
    *
    * @param string $key
    * @return mixed
    */
    public function getMeta(string $key)
    {
         $value = $this->meta()->where('meta_key', $key)->value('meta_value');

         return maybe_unserialize($value);
    }

        /**
         * Get the User roles.
         *
         * @return array
         */
        public function getRolesAttribute(): array
        {
            return array_keys($this->getMeta('wp_capabilities'));
        }
        
        
        /**
         * Get the User permissions.
         *
         * @return array
         */
        public function getPermissionsAttribute(): array
        {
            // How I can search the user permissions in which table?
        }
        
    }

    class UserMeta extends Model
    {
        protected $table = 'usermeta';
        protected $primaryKey = 'umeta_id';
         /**
         * Get the meta parent.
         *
         * @return \Illuminate\Database\Eloquent\Relations\Relation
         */
        public function user(): Relation
        {
            return $this->belongsTo(User::class, 'user_id');
        }
        
    }

标签: wordpresslaraveleloquentlumeneloquent-relationship

解决方案


推荐阅读