Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit Model in Database Validations dont work #30726

Closed
hersan opened this issue Dec 2, 2019 · 7 comments
Closed

Explicit Model in Database Validations dont work #30726

hersan opened this issue Dec 2, 2019 · 7 comments
Labels

Comments

@hersan
Copy link

hersan commented Dec 2, 2019

  • Laravel Version: 6.6.0
  • PHP Version: 7,2.19
  • Database Driver & Version:

Description:

Documentation says I can do something like the following:
email' => 'unique:App\User,email_address'

But I do a simple test and it fails!

Steps To Reproduce:

Test:
/** @test */
public function it_check_unique_validation()
{
$user = factory(User::class)->create();

    $validator = Validator::make(['email' => $user->email],[
        'email' => 'unique:App\User',
    ]);

    $this->assertTrue($validator->fails());
    $this->assertEquals(
        'The email has already been taken.',
        $validator->errors()->first()
    );
}

Result:
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'lara6
.app\user' doesn't exist

@gocanto
Copy link
Contributor

gocanto commented Dec 2, 2019

PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'lara6
.app\user' doesn't exist

This seems to be related to a misconfiguration of the given model. Also, you would need to specify what column to query against.

@clugg
Copy link
Contributor

clugg commented Dec 2, 2019

This seems to be related to a misconfiguration of the given model.

Could you provide some more information about this? I have been having similar issues. I wrote a more comprehensive explanation in the pull request.

Also, you would need to specify what column to query against.

This is incorrect - if you don't specify a column, the docs state that it will by default query the column with the same name as the input key (i.e. an input named 'email' will query the 'email' column if you don't specify a custom one).

@hersan
Copy link
Author

hersan commented Dec 2, 2019

Hi, Take a closer look at the code and I think the problem is here:
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Validation/Concerns/ValidatesAttributes.php#L816
parseTable() does not consider the case when the model is passed as a string

I also found that there are only tests for the Rule class. This makes me think that this PR was intended only for the use of the Rule class. And that may indicate that the documentation has an error.

@driesvints driesvints added the bug label Dec 2, 2019
@driesvints driesvints changed the title Explicit Model in Database Validatiosn dont work Explicit Model in Database Validations dont work Dec 2, 2019
@ahinkle
Copy link
Contributor

ahinkle commented Dec 7, 2019

Hey there, I would love to investigate this issue further. Do you have an example application you can share that will replicate this issue? - or even a unit test against the framework?

@clugg
Copy link
Contributor

clugg commented Dec 8, 2019

@ahinkle
I started a project from scratch to reproduce the issue. You can find it at clugg/laravel-model-validation. All of the validation code is in the routes/api.php file. The welcome view contains a link to every route for you to see the output, the red links are those that aren't functioning as expected.

There are a fair few routes, as per my comment on the PR: option 1 is using validation rules as a single string delimited by a pipe | (notated by "delimited"), option 2 is using validation rules as strings in an array (notated by "array") and option 3 is using the exists/unique rules as instances of the class (notated by "instance"). For complete transparency I've also included examples where the column is explicitly defined as opposed to implied by the rule's name (notated by "explicit").

Options 1 and 2 both produce the same output:

"SQLSTATE[HY000]: General error: 1 no such table: App\\Models\\User (SQL: select count(*) as aggregate from \"App\\Models\\User\" where \"id\" = 1)"

Option 3 produces the expected output, given the database is in-memory and hasn't been migrated:

"SQLSTATE[HY000]: General error: 1 no such table: users (SQL: select count(*) as aggregate from \"users\" where \"id\" = 1)"

As you can see, option 3 correctly resolves the table name.

@driesvints
Copy link
Member

PR was merged.

@hersan
Copy link
Author

hersan commented Dec 10, 2019

@driesvints thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants