djoks's avatar
Level 1

Laravel unique and exists validation - [table_name] class does not exist

Hello, I am having a very weird issue which I do not know how to solve, 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.

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": [..]
  }
}

I have tried using different tables, I have tried it in different controllers but still.

I posted on stackoverflow over a week ago but was not able to get a solutions or even a reason why this is happening.

0 likes
1 reply

Please or to participate in this conversation.