Call to a member function relationLoaded() on string on voyage admin

删除回忆录丶 提交于 2019-12-11 03:36:47

问题


I installed the voyager admin successfully from here

On my client page, I created a custom registration which is derived from auth. I can register the user successfully.

After I installed the voyager admin, I added a new user on client's registration form. Then, when i tried to access the http://localhost:8000/admin and then error occurred as seen on the image.

Below is the image of the line 53:

And below is the entire code of VoyagerUse.php

<?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;
}
}

回答1:


As mentioned in the VoyagerUser in line 53 :

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

The role here is considered as a relation not a field :)

and the error

Call to a member function relationLoaded() on string

means that you have the role as attribute in the User Model

So all you have to do is rename the role attribute to something else and everything will work perfectly ;)




回答2:


You should simply change the name of your attribute named role because Voyager add a relation named role as well



来源:https://stackoverflow.com/questions/46491081/call-to-a-member-function-relationloaded-on-string-on-voyage-admin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!