Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pedroroccon's avatar

Problem with unique field validation on update

Hello guys! Thanks for the interest.

Well, I have a question. In my application I have a form for populate my products table. Each product may have a unique ID and a unique code.

I created a ProductRequest file with the follow code:

    public function rules()
    {
        return [
            'code' => 'required|min:4|unique:products,code,:id', 
            'title' => 'required', 
        ];
    }

For inserting a new record the validation is working, but when I try to update the record, the Laravel gives me the following message:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: ":id" (SQL: select count(*) as aggregate from "products" where "code" = TST0005BYUS and "id" <> :id)

I try to search the solution but no success. What I'm doing wrong?

Thanks for the help! :)

0 likes
17 replies
mstnorris's avatar
Level 55

http://laravel.com/docs/5.1/validation#rule-unique

Forcing A Unique Rule To Ignore A Given ID:

Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address. You only want to throw a validation error if the user provides an e-mail address that is already used by a different user. To tell the unique rule to ignore the user's ID, you may pass the ID as the third parameter:

'email' => 'unique:users,email_address,'.$user->id
16 likes
amritshrestha's avatar

what if the table name is menu_categories

'name' => 'unique:menu_categories,name,'.$menu_category->id this wont work if table name is with two word

1 like
pedroroccon's avatar

@mstnorris Thank you very much for the answer. I made a little change in my code and it WORKS now!

    public function rules()
    {
        return [
            'code' => 'required|min:4|unique:products,code,' . $this->product->id, 
            'title' => 'required', 
        ];
    }

Thank you!

10 likes
technoigniters's avatar

@pedroroccon getting error - "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'" while my table has 'dt_id' instead of 'id'. Kindly suggest. Thanks.

jokamjohn's avatar

@pedroroccon Hello, i have done the exact thing you did but, its not working for me. cani see the full code thanks

bobbybouwmann's avatar

@jokamjohn Please create your own thread. Threads that are marked answered are not watched by everyone! Also make sure that you tell you what is not working and what you have tried!

timmy1420's avatar

This is how I did it in UserRequest.php

class UserRequest extends FormRequest {

public function rules() {

    if( count( \Request::segments() ) > 1 ) {

        $user_id = \Request::segments()[1];

        return [
            'name' => 'required|min:3|unique:users,name,' . $user_id,
            'address' => 'required|min:3|unique:users,address,' . $user_id,
            'email' => 'indisposable|unique:users,address,' . $user_id,
        ];
    }
}

}

2 likes
Gutlu's avatar

Can some one please detail how to create a validation rule for mongodb for update function with below field say a) dept_id (primary key) b) dept_name c) dept_location with both the field b and c will be required and unique

zizi_ove's avatar

I think another solution in the update process is to make the input field related to email in the edit form, "disabled". in this way, the email is not passed anymore.

hope it helps.

1 like
johnef_sh's avatar

if some one has what I got in Laravel 8 I had to validated two fields ['ar', 'en'] as unique names

and here is what worked for me

return [
                    'name_ar'          => [
                        'required',
                        Rule::unique( 'product_category_translations', 'name' )->ignore( $this->id, 'product_category_id' )->where( function ( $query ) {
                            return $query->where( 'locale', 'ar' );
                        } )
                    ],
                    'name_en'          => [
                        'required',
                        Rule::unique( 'product_category_translations', 'name' )->ignore( $this->id, 'product_category_id' )->where( function ( $query ) {
                            return $query->where( 'locale', 'en' );
                        } )
                    ],
                    'background_color' => 'required',
                ];
1 like

Please or to participate in this conversation.