0
votes

Currently updating Laravel from v5.2 to 5.5 and can't figure out how I get the "unique" rule working on update.

This is my rule that works nice on Laravel 5.2

  public function rules()
{
    $retVal = [
        'username' => 'required|alpha_dash|min:4|max:30',
        'password' => 'min:6|confirmed',
        'password_confirmation' => 'min:6',
        'email' => 'required|email|unique:users,email,' . $user->id,
        'roles_list' => 'required',
    ];



    if (stristr($this->get('username'), '@'))
    {
        $retVal['username'] = 'required|email';

    }

    return $retVal;
}

But now in Laravel 5.5 I get always an "email unique"-error on update. When I type in the user-id manually it works. Like described in the Laravel docs it should work:

https://laravel.com/docs/5.5/validation#form-request-validation

I have no clue how to get the $user object accessible in my form request. Thank you very much in advance for assist !

1
Seems like $user is never assigned ? Is this the authenticated user? In that case you should try with Auth::user()->idEsteban Garcia

1 Answers

0
votes

Your $user variables seems null.

If you're trying to retrieve the currently authenticated user in your Request class, you can try this:

$user = $this->user();

Next, you have to change your email rules a bit:

'email' => 'required|email|unique:users,email,' . ($user ? $user->id : 0),