Laravel 4 validation email unique constraint

后端 未结 2 687
无人及你
无人及你 2020-12-20 22:16

I\'m writing a Laravel 4 app that has a users table with the usual contact info. In my users model, my validation for the email specifies \'e

相关标签:
2条回答
  • 2020-12-20 22:58

    Unique Validation Syntax

    unique:table,column,except,idColumn

    Try this to exclude the current email

    unique:'users', 'email', Auth::user()->email

    0 讨论(0)
  • 2020-12-20 23:21

    The third parameter to the unique rule allows you to specify an id of a record to ignore. When you're editing a user, you want your unique validation rule to ignore the value contained by the id of the user you are editing.

    'email'=>'required|email|unique:users,email,'.$userId
    

    You can see the docs on the validation rule here.

    The trickiest part you'll run into is figuring out how to edit your rule to provide the id of the user you're editing. That all depends on how you have your rules set up and where you're doing your validation.

    0 讨论(0)
提交回复
热议问题