Ad

Laravel Edit User Data But Show Input Email Error. The Email Has Already Been Taken

- 1 answer

I add and edit in the admin dashboard users also delete. Everything works except editing. In editing, he asks to change the email every time. But I don’t know how to make it work. For example, I want to change only the name or password. And so that it is not necessary to change the email.

This me update and edit function `

public function edit($id)
{
    $object = Specialist::findOrFail($id);
    return view('Backend.specialists.edit', compact('object'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\Specialist  $specialist
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    $data = $request->validate([
        'name' => 'required|string',
        'email' => 'required|email|unique:users',
        'password' => 'required|string|min:8|confirmed'
    ]);

    $object = Specialist::findOrFail($id);
    $object->groups = 3;
    $object->password = \Hash::make($data['password']);
    $object->fill(request()->except('password'));

    $object->save();

    return redirect()->route('specialists.index');
}

`

Input Error Image

Ad

Answer

You try to write an email address into a table where the same value does still exists (as the same record).

I can't test the validation right not but try to use the ignore rule:

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\Specialist  $specialist
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    $object = Specialist::findOrFail($id);

    $data = $request->validate([
        'name' => 'required|string',
        'email' => [
            'required',
            Rule::unique('users')->ignore($object->id),
        ],
        'password' => 'required|string|min:8|confirmed'
    ]);

    $object->groups = 3;
    $object->password = \Hash::make($data['password']);
    $object->fill(request()->except('password'));

    $object->save();

    return redirect()->route('specialists.index');
}
Ad
source: stackoverflow.com
Ad