Jonathon
Jonathon

Reputation: 16283

Laravel validation: unique rule 4th parameter

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:

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

Upvotes: 0

Views: 5212

Answers (3)

cfv1000
cfv1000

Reputation: 463

  • $ARG1 - looking in this table
  • $ARG2 - checking against this column. Defaults to validation key (eq: email)
  • $ARG3 - Aditional WHERE NOT value.
  • $ARG4 - Aditional WHERE NOT field. Defaults to Primary Key
  • $ARG5 - Aditional WHERE field 1 - Aditional WHERE value.

Upvotes: 4

user4328943
user4328943

Reputation:

you are correct, 4th parameter is column name of id, if different from 'id' as shown here:

https://github.com/laravel/framework/blob/4.2/src/Illuminate/Validation/Validator.php#L991

Upvotes: 1

Jonathon
Jonathon

Reputation: 16283

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'

Upvotes: 4

Related Questions