Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #272
    dan
    Participant

    Hi,

    I have a situation where on my edit form where two fields will be submitted namely, name and email. On registration, the email was configured to be unique.

    Now when I change the name but the email is the same and submit it , its not going to update citing “the email already exists”. Makes sense because the same already live in the database.

    My validation:

    public function update(Request $request, User $user) {

    $update = $request -> validate([
    ‘name’ => [‘required’, ‘max:255′],
    ’email’ => [‘required’, ’email’, ‘unique:users’],
    ]);

    $user -> update($update);

    }

    • This topic was modified 1 year, 3 months ago by dan.
    • This topic was modified 1 year, 3 months ago by roel.
    #274

    Hello, add the Rules class and this to the email validation

    use Illuminate\Validation\Rules;

    public function update(Request $request, User $user) {

    $update = $request -> validate([
    ‘name’ => [‘required’, ‘max:255′],
    ’email’ => [‘required’, ’email’, Rule::unique(User::class)->ignore($user->id)],
    ]);

    $user -> update($update);

    }

    What it does is it ignore the unique validation method if the id matches the user being specified injected.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.