Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Unique and Exists Validation - Class Does Not Exist

Tags:

laravel

lumen

Okay I use lumen 5.4 and I am a little confused about something, I have always used these two validations like this:

$rules = [
    "field1" => "required|exists:users",
    "field2" => "required|unique:users"
];
$messages = [
    "field1.exists" => "Lorem ipsum dolor sit amet.",
    "field2.unique" => "Lorem ipsum dolor sit amet."
];

$validator = Validator::make($request->all(), $rules, $messages);

if ($validator->fails()) {
    return response($validator->errors(), 400);
}

just like the doc says, where "users" is the name of the table in the database. However now all of a sudden I get

Error 500 Class users does not exist

This confused me, checked the docs again and it says to use the table name, so I just assumed that there's been some update and it now uses the Model name instead so I tried that.

Thing is I have all my Models in a Models folder with the following namespace: App\Models;

So then I tried:

$rules = [
    ‘field1’ => "required|exists:User",
    ‘field2’ => "required|unique:User"
];

and

$rules = [
    ‘field1’ => "required|exists:\App\Models\User",
    ‘field2’ => "required|unique:\App\Models\User"
];

and

$rules = [
    ‘field1’ => "required|exists:App/Models/User",
    ‘field2’ => "required|unique:App/Models/User"
];

and

$rules = [
    ‘field1’ => "required|exists:App\\Models\\User",
    ‘field2’ => "required|unique:App\\Models\\User"
];

None of these worked, I have been coding in Java for a while now so it's possible that I may have forgotten my stuff and I am doing something wrong here.

Edit: Exception:

{
  "message": "Class users does not exist.",
  "code": -1,
  "status_code: 500,
  "debug": {
    "line": 334,
    "file": "/home/xxxxx/xxxxx/vendor/laravel-doctrine/orm/src/IlluminateRegistry.php",
    "class": "ReflectionException",
    "trace": [
    "#0 /home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/IlluminateRegistry.php(334): ReflectionClass->__construct('users')",
  "#1 /home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php(123): LaravelDoctrine\\ORM\\IlluminateRegistry->getManagerForClass('users')",
  "#2 /home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php(92): LaravelDoctrine\\ORM\\Validation\\DoctrinePresenceVerifier->getEntityManager('users')",
  "#3 /home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php(45): LaravelDoctrine\\ORM\\Validation\\DoctrinePresenceVerifier->select('users')",
  "#4 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Concerns/ValidatesAttributes.php(612): LaravelDoctrine\\ORM\\Validation\\DoctrinePresenceVerifier->getCount('users', 'email', 'xxxxxx@rock...', NULL, NULL, Array)",
  "#5 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(338): Illuminate\\Validation\\Validator->validateUnique('email', 'xxxxxx@rock...', Array, Object(Illuminate\\Validation\\Validator))",
  "#6 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(253): Illuminate\\Validation\\Validator->validateAttribute('email', 'Unique')",
  "#7 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(278): Illuminate\\Validation\\Validator->passes()",
  "#8 /home/xxxxxx/xxxxxx/app/Http/Controllers/AuthController.php(50): Illuminate\\Validation\\Validator->fails()",
  "#9 [internal function]: App\\Http\\Controllers\\AuthController->register(Object(Dingo\\Api\\Http\\Request))",
  "#10 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(29): call_user_func_array(Array, Array)",
  "#11 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()",
  "#12 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Laravel\\Lumen\\Application), Array, Object(Closure))",
  "#13 /home/xxxxxx/xxxxxx/vendor/illuminate/container/Container.php(531): Illuminate\\Container\\BoundMethod::call(Object(Laravel\\Lumen\\Application), Array, Array, NULL)",
  "#14 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(741): Illuminate\\Container\\Container->call(Array, Array)",
  "#15 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(707): Laravel\\Lumen\\Application->callControllerCallable(Array, Array)",
  "#16 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(681): Laravel\\Lumen\\Application->callLumenController(Object(App\\Http\\Controllers\\AuthController), 'register', Array)",
  "#17 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(643): Laravel\\Lumen\\Application->callControllerAction(Array)",
  "#18 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(623): Laravel\\Lumen\\Application->callActionOnArrayBasedRoute(Array)",
  "#19 [internal function]: Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(Object(Dingo\\Api\\Http\\Request))",
  "#20 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))",
  "#21 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/PrepareController.php(45): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Dingo\\Api\\Http\\Request))",
  "#22 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(148): Dingo\\Api\\Http\\Middleware\\PrepareController->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
  "#23 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
  "#24 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))",
  "#25 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Dingo\\Api\\Http\\Request))",
  "#26 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(778): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
  "#27 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(624): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
  "#28 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(598): Laravel\\Lumen\\Application->handleFoundRoute(Array)",
  "#29 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(532): Laravel\\Lumen\\Application->handleDispatcherResponse(Array)",
  "#30 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(781): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}()",
  "#31 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(534): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
  "#32 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Routing/Adapter/Lumen.php(103): Laravel\\Lumen\\Application->dispatch(Object(Dingo\\Api\\Http\\Request))",
  "#33 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Routing/Router.php(513): Dingo\\Api\\Routing\\Adapter\\Lumen->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')",
  "#34 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(126): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))",
  "#35 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(114): Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware\\{closure}(Object(Dingo\\Api\\Http\\Request))",
  "#36 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
  "#37 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(127): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
  "#38 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(103): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))",
  "#39 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(148): Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
  "#40 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
  "#41 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\\Http\\Request))",
  "#42 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
  "#43 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(778): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
  "#44 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(534): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
  "#45 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(475): Laravel\\Lumen\\Application->dispatch(NULL)",
  "#46 /home/disons5/public_html/xxxxxx/index.php(28): Laravel\\Lumen\\Application->run()",
  "#47 {main}"
    ]
  }
}

After changing my validation rule to use my model instead of table name I now get this error:

{
  "message": "Call to a member function createQueryBuilder() on null",
  "status_code": 500,
  "debug": {
    "line": 93,
    "file": "/home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php",
    "class": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
    "trace": [
      "#0 /home/xxxxxx/xxxxxx/vendor/laravel-doctrine/orm/src/Validation/DoctrinePresenceVerifier.php(45): LaravelDoctrine\\ORM\\Validation\\DoctrinePresenceVerifier->select('App\\\\Models\\\\User')",
      "#1 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Concerns/ValidatesAttributes.php(612): LaravelDoctrine\\ORM\\Validation\\DoctrinePresenceVerifier->getCount('App\\\\Models\\\\User', 'email', 'xxxxxx@rock...', NULL, NULL, Array)",
      "#2 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(338): Illuminate\\Validation\\Validator->validateUnique('email', 'xxxxxx@rock...', Array, Object(Illuminate\\Validation\\Validator))",
      "#3 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(253): Illuminate\\Validation\\Validator->validateAttribute('email', 'Unique')",
      "#4 /home/xxxxxx/xxxxxx/vendor/illuminate/validation/Validator.php(278): Illuminate\\Validation\\Validator->passes()",
      "#5 /home/xxxxxx/xxxxxx/app/Http/Controllers/AuthController.php(50): Illuminate\\Validation\\Validator->fails()",
      "#6 [internal function]: App\\Http\\Controllers\\AuthController->register(Object(Dingo\\Api\\Http\\Request))",
      "#7 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(29): call_user_func_array(Array, Array)",
      "#8 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()",
      "#9 /home/xxxxxx/xxxxxx/vendor/illuminate/container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Laravel\\Lumen\\Application), Array, Object(Closure))",
      "#10 /home/xxxxxx/xxxxxx/vendor/illuminate/container/Container.php(531): Illuminate\\Container\\BoundMethod::call(Object(Laravel\\Lumen\\Application), Array, Array, NULL)",
      "#11 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(741): Illuminate\\Container\\Container->call(Array, Array)",
      "#12 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(707): Laravel\\Lumen\\Application->callControllerCallable(Array, Array)",
      "#13 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(681): Laravel\\Lumen\\Application->callLumenController(Object(App\\Http\\Controllers\\AuthController), 'register', Array)",
      "#14 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(643): Laravel\\Lumen\\Application->callControllerAction(Array)",
      "#15 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(623): Laravel\\Lumen\\Application->callActionOnArrayBasedRoute(Array)",
      "#16 [internal function]: Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(Object(Dingo\\Api\\Http\\Request))",
      "#17 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))",
      "#18 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/PrepareController.php(45): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Dingo\\Api\\Http\\Request))",
      "#19 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(148): Dingo\\Api\\Http\\Middleware\\PrepareController->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
      "#20 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
      "#21 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))",
      "#22 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Dingo\\Api\\Http\\Request))",
      "#23 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(778): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
      "#24 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(624): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
      "#25 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(598): Laravel\\Lumen\\Application->handleFoundRoute(Array)",
      "#26 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(532): Laravel\\Lumen\\Application->handleDispatcherResponse(Array)",
      "#27 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(781): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}()",
      "#28 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(534): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
      "#29 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Routing/Adapter/Lumen.php(103): Laravel\\Lumen\\Application->dispatch(Object(Dingo\\Api\\Http\\Request))",
      "#30 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Routing/Router.php(513): Dingo\\Api\\Routing\\Adapter\\Lumen->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')",
      "#31 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(126): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))",
      "#32 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(114): Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware\\{closure}(Object(Dingo\\Api\\Http\\Request))",
      "#33 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
      "#34 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(127): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
      "#35 /home/xxxxxx/xxxxxx/vendor/dingo/api/src/Http/Middleware/Request.php(103): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))",
      "#36 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(148): Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
      "#37 [internal function]: Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
      "#38 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\\Http\\Request))",
      "#39 /home/xxxxxx/xxxxxx/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
      "#40 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(778): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
      "#41 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(534): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure))",
      "#42 /home/xxxxxx/xxxxxx/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(475): Laravel\\Lumen\\Application->dispatch(NULL)",
      "#43 /home/xxxxxx/public_html/xxxxxx/index.php(28): Laravel\\Lumen\\Application->run()",
      "#44 {main}"
    ]
  }
}

Honestly I'm not even familiar with laravel-doctrine I just follow the laravel/lumen docs always and that's what I've always used to build my apps.

like image 220
user3718908x100 Avatar asked May 06 '17 16:05

user3718908x100


People also ask

How do I ignore unique validation in laravel?

use Illuminate\Validation\Rule; Validator::make($data, [ 'email' => [ 'required', Rule::unique('users')->ignore($user->id), ], ]);

How can I check email is valid in laravel?

You can check if an email is valid or not in Laravel using the validate method by passing in the validation rules. You can validate your email addresses using one or more of the validation rules we have discussed. Or as previously demonstrated, you can write your own custom validation method.


1 Answers

I'm assuming that you're using laravel-doctrine package (from that stack trace). The documentation said that to do unique validation, you should use 2 arguments:

/**
 * Store a new blog post.
 *
 * @param  Request  $request
 * @return Response
 */
public function store(Request $request)
{
    $this->validate($request, [
        'username' => 'required|unique:App\User,username',
    ]);
}

The exists validation:

/**
 * Store a new blog post.
 *
 * @param  Request  $request
 * @return Response
 */
public function update($id, Request $request)
{
    $this->validate($request, [
        'username' => 'required|exists:App\User,username',
    ]);
}

So in your case, it would be:

$rules = [
    "field1" => "required|exists:App\User,field1",
    "field2" => "required|unique:App\User,field2"
];
$messages = [
    "field1.exists" => "Lorem ipsum dolor sit amet.",
    "field2.unique" => "Lorem ipsum dolor sit amet."
];

$validator = Validator::make($request->all(), $rules, $messages);

if ($validator->fails()) {
    return response($validator->errors(), 400);
}

UPDATE

If you get error: Call to a member function createQueryBuilder() on null, then you forget to decorate your Doctrine Metadata. In your case, you need to update App\Models\User class:

use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    public $id;

    /**
     * @ORM\Column(type="string")
     */
    public $field1;

    /**
     * @ORM\Column(type="string")
     */
    public $field2;
}
like image 170
krisanalfa Avatar answered Sep 28 '22 05:09

krisanalfa