代码之家  ›  专栏  ›  技术社区  ›  smzapp

对voyage admin上的字符串调用成员函数relationLoaded()

  •  2
  • smzapp  · 技术社区  · 8 年前

    我从中成功安装了voyager admin here

    http://localhost:8000/admin 然后发生了错误,如图所示。

    enter image description here

    下面是第53行的图像:

    enter image description here

    <?php
    
    namespace TCG\Voyager\Traits;
    
    use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
    use TCG\Voyager\Facades\Voyager;
    use TCG\Voyager\Models\Role;
    
    /**
     * @property  \Illuminate\Database\Eloquent\Collection  roles
     */
    trait VoyagerUser
    {
    public function role()
    {
        return $this->belongsTo(Voyager::modelClass('Role'));
    }
    
    /**
     * Check if User has a Role(s) associated.
     *
     * @param string|array $name The role to check.
     *
     * @return bool
     */
    public function hasRole($name)
    {
        if (!$this->relationLoaded('role')) {
            $this->load('role');
        }
    
        return in_array($this->role->name, (is_array($name) ? $name : [$name]));
    }
    
    public function setRole($name)
    {
        $role = Voyager::model('Role')->where('name', '=', $name)->first();
    
        if ($role) {
            $this->role()->associate($role);
            $this->save();
        }
    
        return $this;
    }
    
    public function hasPermission($name)
    {
        if (!$this->relationLoaded('role')) {
            $this->load('role');
        }
    
        if (!$this->role->relationLoaded('permissions')) {
            $this->role->load('permissions');
        }
    
        return in_array($name, $this->role->permissions->pluck('key')->toArray());
    }
    
    public function hasPermissionOrFail($name)
    {
        if (!$this->hasPermission($name)) {
            throw new UnauthorizedHttpException(null);
        }
    
        return true;
    }
    
    public function hasPermissionOrAbort($name, $statusCode = 403)
    {
        if (!$this->hasPermission($name)) {
            return abort($statusCode);
        }
    
        return true;
    }
    }
    
    2 回复  |  直到 8 年前
        1
  •  5
  •   Maraboc    8 年前

    如VoyagerUser在第53行中所述:

    if(!$this->role->relationLoaded('permissions')){ ...
    

    此处的角色被视为关系,而不是字段:)

    对字符串调用成员函数relationLoaded()

    意味着您在用户模型中具有角色as属性

    因此,您所要做的就是将角色属性重命名为其他属性,一切都会完美地工作;)

        2
  •  0
  •   Wendinmi    7 年前