majidarif majidarif - 2 years ago 371
PHP Question

Laravel validation exists where NOT

Based on the documentations. Exists can have 4 parameters:


The question is, What if I wanted it to be where is not. Like where is not 0.

$validator = Validator::make(
'groups' => $groups
'groups' => 'required|exists:groups,jid,parent,!=,0'

Answer Source

You can use something like this:

Validator::extend('not_exists', function($attribute, $value, $parameters)
    return DB::table($parameters[0])
        ->where($parameters[1], '=', $value)
        ->andWhere($parameters[2], '<>', $value)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download