I have 2 columns in table servers.
I have columns ip
and hostname
.
I have validation:
'data.ip' => ['required', 'unique:servers,ip,'.$this->id]
This working only for column ip
. But how to do that it would work and for column hostname
?
I want validate data.ip with columns ip
and hostname
.
Because can be duplicates in columns ip and hostname, when user write ip.
Advertisement
Answer
You can use Rule::unique
to achieve your validation rule
$messages = [ 'data.ip.unique' => 'Given ip and hostname are not unique', ]; Validator::make($data, [ 'data.ip' => [ 'required', Rule::unique('servers')->where(function ($query) use($ip,$hostname) { return $query->where('ip', $ip) ->where('hostname', $hostname); }), ], ], $messages );
edit: Fixed message assignation