Chris Townsend
Chris Townsend

Reputation: 2716

Laravel 5 unique validation in edit field with different primary key

I am using the request feature in Laravel 5, and I can create a new whitelist item which works.

This is the rules for the edit function, and I need it to ignore the unique email field, when this hasn't been altered.

I am using what it says in the docs and setting my rules as follows

public function rules()
{
    return [
        'name' => 'required',
        'email'=> 'required|email|unique:whitelists,whitelistID,'.$this->segment('2'),
        'level' => 'required',
        'position' => 'required'
    ];
}

I am getting the error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from `whitelists` where `whitelistID` = [email protected] and `id` <> 3)

The column uses whitelistID as its primarykey and this is set in the Whitelist model using

protected $primaryKey = 'whitelistID';

Any idea why this is defaulting to 'id'?

Upvotes: 2

Views: 1876

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152860

You are wrong with the arguments of unique. This should work better:

'email'=> 'required|email|unique:whitelists,email,'.$this->segment('2').',whitelistID',

Upvotes: 3

Related Questions