【问题标题】:Laravel - SuperAdmin can't see other rolesLaravel - 超级管理员看不到其他角色
【发布时间】:2021-04-28 02:41:12
【问题描述】:

我在我的用户模型中添加了这个:

    private $rank;

    public function isSuperAdmin(): bool {

        if ($this->rank >= 3) {
            return true;
        }

        return false;
    }

    public function isAdmin(): bool
    {
        if ($this->rank == 2 || $this->isSuperAdmin()) {
            return true;
        }

        return false;
    }

    public function isCustomer(): bool {
        if ($this->rank == 1|| ($this->isSuperAdmin() || $this->isAdmin())) {
            return true;
        }

        return false;
    }

我的排名'3'等于isSuperAdmin() 函数。因此,当我执行以下操作时:

if (Auth::user()->isSuperAdmin())

这按预期工作。但是,当我作为超级管理员尝试执行以下操作时:

if (Auth::user()->isAdmin())

它根本不起作用,当我拥有超级管理员角色时,Laravel 不会在我的视图中显示@if(Auth::user()->isAdmin()) 之间的任何内容。这对所有角色都是一样的,我不明白为什么..我做错了什么?

附言。我还在isAdmin 函数中尝试了以下方法,但也不起作用:

if ($this->rank >= 2 || $this->isSuperAdmin()) {

【问题讨论】:

  • It doesn't work at all 是什么意思?究竟什么不起作用?
  • @codedge Laravel 在我拥有 SuperAdmin 角色时不会显示 @if(Auth::user()->isAdmin) 之间的任何内容。这对所有角色都是一样的。
  • dd($this->rank) 的输出是什么?
  • 我修好了。原来 $this->rank 由于私有 $rank 始终为空。当我删除它时,一切都按预期工作。

标签: laravel permissions admin roles


【解决方案1】:

在我删除 private $rank 后,一切都按预期工作。

【讨论】:

    猜你喜欢
    • 2014-02-28
    • 2016-03-05
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    相关资源
    最近更新 更多