setting rule for unique name on existing row

Hello, in laravel 5.4 setting rule for unique name like

			$rules = array(
				'name'      => 'required|unique:department,name',
			Works ok inserting new department , but it finds itself when updating existing department and in profile I see:
select count(*) as aggregate from `jsn_department` where `name` = 'Department_111222'

I need also condition

... and department_id != 5

How to make it ?

Thanks !

lagbox replied 1 year ago

Laravel 5.4 - Validation - Unique Rule

Laravel 5.2 - Validation - Unique Rule <- similar formatting to what you are using

its in the validation docs for the unique rule - "Forcing A Unique Rule To Ignore A Given ID"

sergeynilov replied 1 year ago

Thanks for answer! If found that similar rule works ok when I need "=" in sql statement


and I have sql :

select count(*) as aggregate from `jsn_department` where `name` = 'Department_111222' and `department_id` = '1'

But I failed to set condition with "!=" : Setting rule as :


I got error:

Unknown column 'department_id!=' in 'where clause' (SQL: select count(*) as aggregate from `jsn_department` where `name` = Department_111222 and `department_id!=` = 1)

As I see "department_id!=" is wrapped with "`" char and "=" is set anyway...

Which is the right syntax ?

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us