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'
)
);
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;
});
You can use unique
Example
'email' => 'unique:users,email_address,NULL,id,account_id,1'
In the rule above, only rows with an account_id of 1 would be included in the unique check.
http://laravel.com/docs/4.2/validation#rule-unique
With Laravel 5.2 or later you can prepend the values to be checked against with a !
:
'groups' => 'required|exists:groups,jid,parent,!0'
The same login goes for unique
, only you need the extra parameters:
'groups' => 'required|unique:groups,jid,NULL,id,parent,!0'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With