Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

geoffrey
geoffrey
28,736 Points

Laravel - Unique email & update

I'm stuck with laravel and its unique verification on fields. (in this case, on the email adress's field');

Hampton Paulk Could you help me please ? (thank you for the great introduction to laravel).

When creating a new user:

No problem, It works, It detects if the e-mail has already been taken, and thus display the right message accordingly.

When updating the user information:

It doesn't work anymore.

When creating, my rule for the email field is this one:

    <?php

     $rules = array(
            'email'=>'required|email|unique:users' 
           );
?>

I had to change it to that one for my Update method:

    <?php

    $rules = array(
             'email'=>'required|email' 
                 );
    ?>

If I leave the unique:users rule, It doesn't work, which is logical. Because it detects the entry as being already used...

So, setting this rule with no unique:users rule works, It accepts the update with the same e-mail address left.

The problem is when I modify the email field and put inside this one an email address affected to another user and so already existing in the database.

Laravel display me this message, because the email column has a unique value in my users table.

Illuminate \ Database \ QueryException (23000)

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'geogeoPaige Coley

So how to deal with the error message, and display once again this time in my update form, just next to the email field that the email adress is already being used by another user ?

1 Answer

geoffrey
geoffrey
28,736 Points

I've finally found the way to do it, coudln't figure out how to do it reading the documentation, I was a bit lost.

The rule should be set this way for the update form.

<?php
$rules = array(
        'lastName'=>'required',
        'firstName'=>'required',
        'function'=>'required',
        'email'=>'required|email|unique:users,email,'.$id 
        );
?>

email is the name of my column where I put all my users's email, inside my users table. $id the $id of the current user we are editing the information, basically from what I understand.

This way, you can save the edited information for the user without having the message telling the email address is being used, and at the same time if you specify the e-mail address of someone else, It'll notify you the e-mail is already taken.