Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do "except" and "idColumn" refers in "unique:table,column,except,idColumn"? from Laravel docs

To validate the update of the e-mail of a user already registered I have the next function to exclude the "unique" rule for the current User:

public function updateRules() {
    return [
        'name'  => 'required',
        'email' => 'required|unique:users,email,'.$this->id,
    ];
}

In the Laravel docs https://laravel.com/docs/5.7/validation#rule-unique I found the syntax unique:table,column,except,idColumn with 4 parameters:

  • table: refers to the table name "users"
  • column: refers the column name "email"
  • except: I'm taking it as the id of the Model instance I want to exclude from the "unique" verification
  • idColumn: I have no idea about this

Someone could clarify what do except and idColumn refers to?

Note for those obsessed with duplicate questions: I'm not asking how to do the rule exclusion for the update, because it seems to work just fine, I've read those questions and answers. I'm making helper functions and I need to know exactly what those two parameters means.

like image 706
Alcides Avatar asked Dec 04 '18 16:12

Alcides


1 Answers

So you are right about the except parameter, it is the id that you want to be excluded from the check.

And idColumn is optional in case your column is not called id but user_id for example. So you will use:

'required|unique:users,email,'.$this->id . ',user_id';
like image 62
nakov Avatar answered Nov 15 '22 04:11

nakov