Andy Holmes Andy Holmes - 2 months ago 32
PHP Question

Laravel 5.3 - Unique field enforces rule on update

I'm building a CRM inside Laravel 5.3. I have a

rule
like this in my
ClientRequest
rules
array

'company_name' => 'required|unique:clients',


Which means that the Company Name for the Client you're adding needs to be unique in the database. This works, however when I try to update the already existing client, it throws an error saying that the company name needs to be unique.

I tried doing
'company_name' => 'required|unique:clients,company_name,{$this->client_uid},client_uid',


As
client_uid
is being sent via my
PATCH
request. I was trying to do it that if the
client_uid
being sent with the form matches the
client_uid
of the record just ignore the error but I can't get that bit to work.

Any ideas?

Andy

Answer

Try making your single quotes, double quotes. If you're using single quotes it will not recognize the $this->client_uid as php.

'company_name' => "required|unique:clients,company_name,{$this->client_uid},client_uid"

Another optie could be (but I prefer the above):

'company_name' => 'required|unique:clients,company_name,'.$this->client_uid.',client_uid'
Comments