【问题标题】:Laravel Global Scopes : ErrorException Undefined property: App\Models\User::$companyLaravel 全局范围:ErrorException 未定义属性:App\Models\User::$company
【发布时间】:2021-07-15 14:44:37
【问题描述】:

我正在开发一个多公司应用程序,我想根据登录的用户类型过滤数据.. [总部管理员或标准用户]

所以我希望总公司能够看到所有分公司的报告,所以在总公司简介中,我在注册时自动生成了一个代码。因此,所有注册后的分支机构都需要该代码,以便将它们链接到总公司。

这是我得到的错误。

ErrorException
Undefined property: App\Models\User::$company

我想设置一个全局范围,以便总部管理员可以访问分公司,包括数据。

/ 公司模型全球范围/


    public static function boot()
    {
        parent::boot();

        if(Auth::user()->user_role_id >= 4 && Auth::user()->access_hq == 'YES'){
            static::addGlobalScope('hq_code', function (Builder $builder) {
                $builder->where('hq_code', Auth::user()->company['hq_code']);
            });
        } elseif(Auth::user()->user_role_id >= 4 && Auth::user()->access_hq !== 'YES'){
            static::addGlobalScope('id', function (Builder $builder) {
                $builder->where('id', Auth::user()->company_id);
            });
        }
    }

/ 模型关系/

/**
* User Model
*/
    public function company()
    {
        return $this->belongsTo(\App\Models\Company::class);
    }

/**
* Company Model
*/

    public function users()
    {
        return $this->hasMany(\App\Models\User::class);
    }

编辑

如果我以 Admin/HQ Admin 身份登录的查询进行调试,我仍然会收到错误,但是当我以分支用户/标准用户身份登录时,我会收到此错误

Illuminate\Database\Eloquent\Builder {#1955 ▼
  #query: Illuminate\Database\Query\Builder {#1962 ▶}
  #model: App\Models\Company {#1948 ▶}
  #eagerLoad: []
  #localMacros: array:4 [▶]
  #onDelete: Closure(Builder $builder) {#1944 ▶}
  #passthru: array:19 [▶]
  #scopes: array:2 [▼
    "Illuminate\Database\Eloquent\SoftDeletingScope" => Illuminate\Database\Eloquent\SoftDeletingScope {#1765 …}
    "id" => Closure(Builder $builder) {#1854 ▼
      class: "App\Models\Company"
      file: "DRIVE:\webapp\app\Models\Company.php"
      line: "70 to 72"
    }
  ]
  #removedScopes: []
}

编辑 - dd(auth()->user())

App\Models\User {#1938 ▼
  #fillable: array:4 [▶]
  #hidden: array:2 [▶]
  #attributes: array:17 [▶]
  #connection: "mysql"
  #table: "users"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #original: array:17 [▶]
  #changes: []
  #casts: array:1 [▶]
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▶]
  #rememberTokenName: "remember_token"
  #forceDeleting: false
}

【问题讨论】:

  • 这一行是否给您带来了错误? $builder->where('hq_code', Auth::user()->company['hq_code']);你调试了吗
  • 是的,它抛出了一个错误,我确实调试了它,当我以 HQ Admin 身份登录时它仍然给我同样的错误,但如果我以标准用户或分支用户身份登录,我会过滤数据
  • 我认为这是因为Auth::user()->company 在调用Company 时需要启动 Company 但由于我们在in中尚未启动> 靴子。有意义吗?
  • 所以它试图启动公司模型,同时试图过滤未启动的公司模型??
  • 我不确定您所说的 token 是什么意思,但如果我们彼此理解正确,那就是这个。更准确地说;在addGlobalScope 中添加的范围将在我们加载任何关系->company 之前播放。这意味着属性$company 还不存在。不过我没有解决你的问题。

标签: laravel


【解决方案1】:

问题就在这里,你试图访问你定义的关系但是错误的sintax

Auth::user()->company['hq_code'] 

必须

Auth::user()->company->hq_code

【讨论】:

  • 嗨,它仍然给我一个错误ErrorException Undefined property: App\Models\User::$company
  • @BonganiDlamini 很好,错误发生了变化,这意味着它有效。你能dd(auth()->user());
【解决方案2】:

我不确定这是否是这里最好的实现,但我想做的是在用户表中添加一个带有 hq_code 的列,在注册 HQ Admin User 时,我使用 hq_code 并将其添加到用户配置文件中。

对于标准用户,我将使用默认代码以保持一致性。

我已经使用本地演示数据库进行了测试,我正在实现将总部的所有分支机构都显示在公司列表中的结果。

所以我不确定这个解决方案是否是最好的,但它可以满足我目前的需要。

public static function boot()
{
    parent::boot();

    if (Auth::user()->user_role_id >= 4 && Auth::user()->access_hq == 'YES') {
        static::addGlobalScope('hq_code', function (Builder $builder) {
            $builder->where('hq_code', Auth::user()->hq_code);
        });
    } elseif (Auth::user()->user_role_id >= 4 && Auth::user()->access_hq !== 'YES') {
        static::addGlobalScope('id', function (Builder $builder) {
            $builder->where('id', Auth::user()->company_id);
        });
    }
}

编辑:

我认为我还必须向标准用户添加相同的 HQ 代码,以便 HQ 管理员可以访问或查看属于同一 hq_code 的所有用户。

分支机构管理员可以使用 company_id 列访问属于同一公司的用户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2022-12-09
    • 2019-09-25
    • 2021-06-21
    • 2023-02-06
    相关资源
    最近更新 更多