majidarif majidarif - 7 months ago 138
PHP Question

Laravel validation exists where NOT

Based on the documentations. Exists can have 4 parameters:

exists:table,id,where,0


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

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

Answer

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)
        ->count()<1;
});
Comments