Laravel验证:唯一规则第4个参数 [英] Laravel validation: unique rule 4th parameter

查看:198
本文介绍了Laravel验证:唯一规则第4个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览过Laravel文档,API文档和源代码,我想知道是否有人知道以下唯一规则中的第4个参数id是做什么的?

'email' => 'unique:users,email_address,NULL,id,account_id,1'

我目前对该规则的理解是:

  • users-在此表中查找
  • email_address-对此列进行检查
  • NULL-我们可以在其中指定要忽略的主键/ID值,但是我们没有打扰,因此实际上忽略了此参数
  • id-不确定
  • account_id-其他where子句,这是列名
  • 1-where子句中的account_id的值

文档: http://laravel.com/docs/4.2/validation

负责执行在949行上的功能validateUnique($attribute, $value, $parameters)中的\Illuminate\Validation\Validator中找到的唯一规则验证的功能:

/**
 * Validate the uniqueness of an attribute value on a given database table.
 *
 * If a database column is not specified, the attribute will be used.
 *
 * @param  string  $attribute
 * @param  mixed   $value
 * @param  array   $parameters
 * @return bool
 */
protected function validateUnique($attribute, $value, $parameters)
{
    $this->requireParameterCount(1, $parameters, 'unique');

    $table = $parameters[0];

    // The second parameter position holds the name of the column that needs to
    // be verified as unique. If this parameter isn't specified we will just
    // assume that this column to be verified shares the attribute's name.
    $column = isset($parameters[1]) ? $parameters[1] : $attribute;

    list($idColumn, $id) = array(null, null);

    if (isset($parameters[2]))
    {
        list($idColumn, $id) = $this->getUniqueIds($parameters);

        if (strtolower($id) == 'null') $id = null;
    }

    // The presence verifier is responsible for counting rows within this store
    // mechanism which might be a relational database or any other permanent
    // data store like Redis, etc. We will use it to determine uniqueness.
    $verifier = $this->getPresenceVerifier();

    $extra = $this->getUniqueExtra($parameters);

    return $verifier->getCount(

        $table, $column, $value, $id, $idColumn, $extra

    ) == 0;
}

欢呼

解决方案

嗯,我认为一分钱都没有下降.

如果我错了,请纠正我,但是第4个参数与第3个参数相关,因为它允许我们指定忽略3中指定的ID时要检查的列.如果不是id. /p>

例如,如果主键不是id而是user_id,我们可以这样做:

'email' => 'unique:users,email_address,NULL,user_id,account_id,1'

Having a look through the Laravel docs, API documents and source code I am wondering if anyone knows what the 4th parameter id in the following unique rule is for?

'email' => 'unique:users,email_address,NULL,id,account_id,1'

My current understanding of this rule is:

  • users - looking in this table
  • email_address - checking against this column
  • NULL - would be where we could specify a primary key/ID value to ignore, but we haven't bothered so this parameter is essentially ignored
  • id - unsure
  • account_id - Additional where clause, this is the column name
  • 1 - the value for account_id in the where clause

Documentation: http://laravel.com/docs/4.2/validation

Function responsible for carrying out the unique rule validation found in \Illuminate\Validation\Validator in function validateUnique($attribute, $value, $parameters) on line 949:

/**
 * Validate the uniqueness of an attribute value on a given database table.
 *
 * If a database column is not specified, the attribute will be used.
 *
 * @param  string  $attribute
 * @param  mixed   $value
 * @param  array   $parameters
 * @return bool
 */
protected function validateUnique($attribute, $value, $parameters)
{
    $this->requireParameterCount(1, $parameters, 'unique');

    $table = $parameters[0];

    // The second parameter position holds the name of the column that needs to
    // be verified as unique. If this parameter isn't specified we will just
    // assume that this column to be verified shares the attribute's name.
    $column = isset($parameters[1]) ? $parameters[1] : $attribute;

    list($idColumn, $id) = array(null, null);

    if (isset($parameters[2]))
    {
        list($idColumn, $id) = $this->getUniqueIds($parameters);

        if (strtolower($id) == 'null') $id = null;
    }

    // The presence verifier is responsible for counting rows within this store
    // mechanism which might be a relational database or any other permanent
    // data store like Redis, etc. We will use it to determine uniqueness.
    $verifier = $this->getPresenceVerifier();

    $extra = $this->getUniqueExtra($parameters);

    return $verifier->getCount(

        $table, $column, $value, $id, $idColumn, $extra

    ) == 0;
}

Cheers

解决方案

Ah nuts, I think the penny has just dropped.

Correct me if I'm wrong, but the 4th parameter is related to the 3rd parameter in that it allows us to specify which column we want to check when ignoring the ID specified in 3. If it's not id.

Example, if the primary key was not id and was user_id instead, we could do this:

'email' => 'unique:users,email_address,NULL,user_id,account_id,1'

这篇关于Laravel验证:唯一规则第4个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆