Validation error in Laravel update

Asked

Viewed 374 times

0

Follows the code:

public function update(Request $request, Contact $contact)
{

    $validator = Validator::make($request->all(), [
        'name' => 'required|min:6|max:255',
        'email' => 'required|email|unique:contacts,' . $contact->id
    ]);

    if ($validator->fails()) {
        $errors = $validator->errors();

        return response()->json($errors, 400);
    }

    $contact->update($request->all());

    return response()->json($contact);
}

Url: http://127.0.0.1:8000/api/contacts/1

Method: PUT -> Body -> x-www-form-urlencoded

Error:

Illuminate Database Queryexception: SQLSTATE[42703]: Undefined column: 7 ERROR: column "1" does not exist LINE 1: select Count() as Aggregate from "Contacts" Where "1" = $1 (SQL: select Count() as Aggregate from "Contacts" Where "1" = [email protected]) in file C: Users Free Documents Appimobiliaria api-Rest vendor Laravel framework src Illuminate Database Connection.php on line 669

I’m using the version 6.11.0 of Laravel and bank Postgresql.

  • Put the snippet where the query is, please.

  • Why you awarded the email validator with the id???

  • I am using this video-lesson: https://www.youtube.com/watch?v=nojjKw3ZmRc&list=PLwQZAwVAR7tMF5YTeD9mAN8LGqdcA55Ns&index=10

  • Paste the code of your model contact together with the question

  • 1

    Your table contacts does not have the column 1, you are wrong to generate validation. 'unique:contacts,' . $contact->id search in a specific column. Read https://laravel.com/docs/5.8/validation#Rule-Unique

1 answer

2


The Unique validation receives parameters in order... first the table, second the column and third the id that should be ignored.

'email' => "required|email|unique:contacts,email,$contact->id"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.