简体   繁体   中英

SQLSTATE[42S22]: Column not found: 1054 Unknown column '3' in 'where clause' in laravel

In my laravel application I have simple form to update some existing date.

following is my update method

public function update(Request $request, SalesManager $salesmanager){
        $validatedData = $request->validate([
            'full_name' => 'required|min:5',
            'email' => 'required|email|unique:sales_managers,'.$salesmanager->id,
            'telephone' => 'required|max:10',
            'joined_date' => 'required|date',
            'current_route' => 'required',
            'comments' => 'max:256'
        ],[
            'full_name.required' =>'Full name is required.',
            'full_name.min'=> 'Full name should contain at least 5 minimum letters',
            'email.required'=>'Email is required',
            'email.email'=>'Invalid email format',
            'email.unique' =>'Email address is already exists',
            'telephone.required' => 'Telephone is required',
            'joined_date.required'=>'Joined date is required',
            'current_route.required'=>'Current route is required',
        ]);

        $salesmanager->update($validatedData);
        return redirect()->route('salesmanagers.index')
        ->with('success','Sales manager updated successfully');
    }

and following is my SalesManager model.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

    class SalesManager extends Model
    {
        use HasFactory;
    
        protected $fillable = [
            'full_name', 
            'email', 
            'telephone', 
            'joined_date', 
            'current_route', 
            'comments'
        ];
    }

But when I try to update data from the front end blade, I'm getting the following error....

SQLSTATE[42S22]: Column not found: 1054 Unknown column '3' in 'where clause'

在此处输入图像描述

在此处输入图像描述 I could not find any issue with my controller or model, since the all other methods are working well...

I'm using php 8 and laravel 9

That particular error is being thrown due to the unique rule of the email field in your validation. You're asking it to confirm the email doesn't exist but it's trying to compare the email against an id (in this case the id of your $sales_manager object).

Try replacing your email rule with the following:

'email' => 'required|email|unique:sales_managers,email,'.$salesmanager->id,

In Laravel 5.8+ the introduced ->ignore() with object.

# syntax
Rule::unique('column')->ignore($object)
# code
Rule::unique('email')->ignore($salesmanager)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM