Skip to content

Instantly share code, notes, and snippets.

@nafplann
Last active August 20, 2018 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nafplann/ca843ad56f2dd8bd70430f9c600955df to your computer and use it in GitHub Desktop.
Save nafplann/ca843ad56f2dd8bd70430f9c600955df to your computer and use it in GitHub Desktop.
Validator::extend('object_exists', function ($attribute, $value, $parameters, $validator) {
$class = '\App\\' . ucfirst($parameters[0]);
if (is_array($value)) {
foreach ($value as $id) {
$object = call_user_func([$class, 'find'], $id);
if (is_null($object)) return false;
}
return true;
}
$object = call_user_func([$class, 'find'], $value);
return $object == null ? false : true;
});
Validator::extend('value_unique', function ($attribute, $value, $parameters, $validator) {
$class = '\App\\' . ucfirst($parameters[0]);
$object = call_user_func([$class, 'where'], $attribute, $value)->first();
return $object == null ? true : false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment