【问题标题】:Pivot returns null inside a Laravel Nova custom rule function but not outside itPivot 在 Laravel Nova 自定义规则函数内部返回 null 但不在其外部
【发布时间】:2023-03-06 20:26:02
【问题描述】:

我正在为 Laravel Nova 中的 BelongsToMany 字段设置自定义验证规则,以在数据透视表中查找并与输入的值进行比较。它使用与同一资源的其他字段“共享”的功能。

我在资源Delegation中使用以下代码:

BelongsToMany::make('Authorities')
                ->fields(function ()
                {
                    return [

                        Help::make('Your Limit', "Your own limit for this authority is {$this->displayYourLimit()}. You can only delegate to the limit of your authority."),

                        Number::make('Delegated Limit ($M)', 'delegated_limit')

                            ->rules('required', function ($attribute, $value, $fail)
                            {
                                if ($value > $this->displayYourLimit())
                                {
                                    return $fail('Your value is too high');
                                }
                                …

有问题的函数是:

public function displayYourLimit()
        {
            $id1 = DB::table('delegations')->where('user_id', auth()->user()->id)->value('id');
            $id2 = $this->pivot['authority_id']; //This returns null inside the validation rule but not the Help field
            $result = DB::table('authority_delegation')->where('delegation_id', '=', $id1)->where('authority_id', '=', $id2)->value('delegated_limit');

            return $result;
        }

该函数跨越数据库中的两个表 - delegations,它具有模型 Delegation 和来自 BelongsToMany 与另一个模型 Authority 的关系的轴心。数据透视表称为authority_delegation

$id1 返回当前登录用户的delegations 表的id 列的值。这很好用。

$id2 应该返回附加到Delegation 资源的当前查看的Authorityresource 的authority_delegation 数据透视表的delegation_id 列的值。在我想要得到的以下 url http://localhost:8000/nova/resources/delegations/2/edit-attached/authorities/26?viaRelationship=authorities 中基本上是 26。

最后,$result 返回authority_delegationdelegated_limit 列的值,其中两个第一列的值匹配。如果$id2 发送正确的值,这也可以正常工作。

我希望该函数返回一个浮点数,该浮点数既可用于“帮助”字段中的显示,也可用于“数字”字段中的验证。但是,虽然在帮助字段中完美工作,但在验证中不起作用(预期的验证在required 之后没有效果。通过测试,我看到它在从验证中的枢轴获取$id2 时返回null规则函数。我附上一张图片来说明。图中,绿色圆圈表示在一个字段中正确显示了必要的值,红色圆圈表示它没有正确通过验证。

Illustration of pivot returning null inside a Laravel Nova custom rule function but not outside it

但为什么它只适用于一个领域而不适用于另一个领域呢?

我花了一个下午试图弄清楚。我尝试转换为数组、对象等,但没有任何效果。如果我能以任何其他方式获得当前查看资源的id - 难以捉摸的 26 - 我很想知道!

我在各自的AuthorityDelegation 模型中建立了相关关系:

public function authorities()
    {
        return $this->belongsToMany(Authority::class, 'authority_delegation')->withPivot('delegated_limit', 'further_delegatable', 'master_delegated_limit', 'authority_id', 'delegation_id')->withTimestamps();
    }

and 

public function delegations()
    {
        return $this->belongsToMany(Delegation::class, 'authority_delegation')->withPivot('delegated_limit', 'further_delegatable', 'master_delegated_limit', 'authority_id', 'delegation_id')->withTimestamps();
    }

非常欢迎所有建议!

【问题讨论】:

  • $id2 = $this->pivot['authority_id'];上详细说明一下它来自哪里?
  • 非常感谢您愿意提供帮助!我已经添加了对问题功能和图像的更多解释。

标签: laravel validation eloquent laravel-nova


【解决方案1】:

以防万一有人遇到同样的问题,我就是这样解决的:

不能在闭包中使用$this,PHP 文档对此非常具体。更多详情请看这里https://www.php.net/manual/en/functions.anonymous.php

但是,可以在闭包中使用$request。所以我只是用专用模型替换了数据透视表,并在验证规则中使用了use ($request),如下所示:

Number::make('Delegated Limit')
                ->updateRules('required', function ($attribute, $value, $fail) use ($request) {
                        $id1 = DB::table('delegations')->where('user_id', auth()->user()->id)->value('id');
                        $id2 = $request->authority;
                        $result = DB::table('issuances')->where('delegation_id', '=', $id1)->where('authority_id', '=', $id2)->value('delegated_limit');

                        if ($value > $result)
                        {
                            return $fail('Your value is too high.');
                        }
                    }

而且效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-12
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2020-08-15
    相关资源
    最近更新 更多