Tanvir Ahmed
Tanvir Ahmed

Reputation: 1019

How to resolve "SQLSTATE[42S22]: Column not found"?

I renamed my "id" into "teams_id"

Look at the picture

Now when I update it shows me following error

Look at the image

I tried using the declare it in my model also but it did not work

enter image description here

Here is my update code:

{
     $this->validate(request(), [
        'coach' => 'required',
       'team' => "required|unique:teams,team,$id",
       'manager' => "required|unique:teams,manager,$id",
    ]);

    $team=Team::findorfail($id);

    $team->team = $request->input('team');
    $team->coach = $request->input('coach');
    $team->manager = $request->input('manager');

    $team->save();
    Toastr::success('Team info was updated','Success!');
    return redirect('/teams');

}

Upvotes: 0

Views: 172

Answers (2)

George Hanson
George Hanson

Reputation: 3030

It is failing validation. The unique validation rule is checking against the id column. It doesn't use the model primary key. Instead, change your validation rules to the following:

    $this->validate(request(), [
       'coach' => 'required',
       'team' => "required|unique:teams,team,$id,teams_id",
       'manager' => "required|unique:teams,manager,$id",
    ]);

Upvotes: 2

Theodoros Alexopoulos
Theodoros Alexopoulos

Reputation: 191

The error is probably on your validator

'team' => "required|unique:teams,team,$id",

Check the laravel documentation and try and use

'team' => "required|unique:teams,team,$id,teams_id",

Upvotes: 1

Related Questions