"laravel unique validation on multiple columns" Code 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' => [
        Rule::unique('servers')->where(function ($query) use($ip,$hostname) {
            return $query->where('ip', $ip)
            ->where('hostname', $hostname);

edit: Fixed message assignation

By the_kaba on November 27 2022

Answers related to “laravel unique validation on multiple columns”

Only authorized users can answer the search term. Please sign in first, or register a free account.