【问题标题】:Laravel: How to get GlobalScopes list of a Model?Laravel:如何获取模型的 GlobalScopes 列表?
【发布时间】:2022-08-14 00:12:15
【问题描述】:

我想知道我们如何获得模型的所有 globalScopes 的列表。

假设我们有这些用于 User 模型的 globalScopes:

class User extends Model
{
    use SoftDeletes;

    protected static function boot()
    {
        self::addGlobalScope(new UserTypeScope());

        self::addGlobalScope(new UserStatusScope());

    }
}

现在我想获取 User 模型的所有 GlobalScopes 的列表。

// I want a way to get the name of that two Scope classes

$scopes = User::getScopes();

// $scopes should have UserTypeScope and UserStatusScope

任何帮助将不胜感激

    标签: php laravel builder laravel-9


    【解决方案1】:
    $scopes = (new User())->getGlobalScopes();
    

    或者

    public static function listGlobalScopes()
    {
        return (new self())->getGlobalScopes();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 2021-06-19
      • 2023-01-21
      • 1970-01-01
      相关资源
      最近更新 更多