Home > Enterprise >  how to edit user email in laravel
how to edit user email in laravel

Time:07-21

i want to edit my user email in laravel, but when i submit the form and then it gives me an error message

The selected Email is invalid.

what do I have to do?

whats wrong with this code?

//in Create Function
'email' => 'required|email|unique:users,email',

//in Update Function is this correct?
'email' => 'required|email|exists:users,email',

Controller

This is my userController for update users

public function update(Request $request, User $user)
{
    $validator = Validator::make(
        $request->all(),
        [
            'name' => 'required|string|max:30',
            'email' => 'required|email|exists:users,email',
            'role' => 'required',
            'avatar' => 'required|string|max:150'
        ],
        [],
        $this->attributes()
    );

    if ($validator->fails()) {
        $request['role'] = Role::select('id', 'name')->find($request->role);
        return redirect()
            ->back()
            ->withInput($request->all())
            ->withErrors($validator);
    }

    DB::beginTransaction();
    try {
        $user->update([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
            'avatar' => parse_url($request->avatar)['path'],
        ]);
        $user->syncRoles($request->role);

        Alert::toast(
            __('posts.alert.delete.message.success'),
            'success'
        );

        return redirect()->route('users.index');
    } catch (\Throwable $th) {
        DB::rollBack();
        Alert::toast(
            __('posts.alert.delete.message.error', ['error' => $th->getMessage()]),
            'errors'
        );

        return redirect()
            ->back()
            ->withInput($request->all())
            ->withErrors($validator);
    } finally {
        DB::commit();
    }
}

CodePudding user response:

You still want a unique validator, so the user can't update their account to someone else's email address and cause a conflict.

However, to prevent it from failing when the user isn't updating their email address (it would fail the unique validation, because a record already exists with that email - the user's own), you'll want to exempt the user's current record from the validation.

https://laravel.com/docs/9.x/validation#rule-unique

See "Forcing A Unique Rule To Ignore A Given ID":

// at the top of your file
use Illuminate\Validation\Rule;

'email' => [
    'required',
    'email',
    Rule::unique('users')->ignore($user->id),
]

CodePudding user response:

You're validating the request requiring the email to exist in the table :

'email' => 'required|email|exists:users,email',

You need to specify unique in order to check the value is not used in the table (same as creation)

'email' => 'required|email|unique:users,email',

CodePudding user response:

You can't do either 'email' => 'exists:users,email' or 'email' => 'unique:users,email' for update function. because if you don't change the email you have to submit the old email to the controller which is not "unique" and if you do change it then it doesn't "exist" in the database.

Instead try it like this:

'email' => ['required', 'email', Rule::unique('users', 'email')->ignore($user)],

It means the email should be unique unless it is the email from current user.

See the Laravel docs for more information on this: https://laravel.com/docs/9.x/validation#rule-unique

  • Related