Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. use Illuminate\Support\Arr;
  2. use Illuminate\Database\Eloquent\Relations\Relation;
  3. use Illuminate\Support\Facades\Validator;
  4.  
  5. Validator::extend('poly_exists', function ($attribute, $value, $parameters, $validator) {
  6. // usage:
  7. // 'commentable_id' => 'required|poly_exists:commentable_type
  8.  
  9. if (! $type = Arr:get($validator->getData(), $parameters[0], false)) {
  10. return false;
  11. }
  12.  
  13. if (Relation::getMorphedModel($type)) {
  14. $type = Relation::getMorphedModel($type);
  15. }
  16.  
  17. if (! class_exists($type)) {
  18. return false;
  19. }
  20.  
  21. return ! empty(resolve($type)->find($value));
  22. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement