-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[11.x] Fix Rule::exists: Use correct table name on global models #50367
Conversation
I dunno, it's really hard for me to see what changed in this test given the diff. I'm also pretty hesitant of just removing code like this. |
@taylorotwell New lines are: Is there a way you think I could achieve this without needing me to wrap the original file inside a I really want to commit this change, it would help a lot. And have in mind that if you remove the previously mentioned logic, no test fails, so that part is not tested. Having If the string has And if the string has Following Blame, you can arrive at the original implementation: #30653 by @ahinkle, maybe he knows more and can help, because there are test cases missing too (I am doing my best trying to help with this 🙏). |
Hi @matiaslauriti, Interesting- I haven't run across someone using global models before. Could you share the rationale for adopting a global class modal? I'm not sure a PR would be accepted, implementing such a change might be challenging due to its potentially breaking nature, unless handled with some creativity. |
Of course, thank you for your reply @ahinkle! Currently, in the company I am working for, we do have some old code that was migrated to Laravel a long time ago, we do have "old" models that do not have a namespace, but we do have models that do follow the Laravel convention ( So, I was reviewing a PR and a coworker wrote the literal string rule for I did remember and know that you can pass a But when he tried the code I shared ( My idea of this change is not only to have predictable code (it should work 100% with a Model, it does not matter where it is coming from, if it extends The part that I am now more interested is not exactly "fixing" this unwanted behavior, but also getting to know why it makes use of I honestly would love to see this merged so it is predictable to anyone, but if it is not, I would also be happy if I or someone can add a test that finally identifies what |
Hey @ahinkle! Sorry for the ping, just want to know if you were able to check what I wrote? |
I see my code may not be relevant, but I am trying to contribute, fixing a small issue and adding tests, but it is hard to contribute on details like this when one no one can invest 5 minutes reading what I wrote on the description and the final code (super small) |
I was working on a FormRequest's rule section at my company (using Laravel 9.x) when I tried using a normal
Rule::exists(SomeModel::class, 'some_column')->withoutTrashed()->where('another_column', 1)
but the rule was failing because it tried usingdatabase_name.SomeModel
instead of the correct$table
value forSomeModel
.I did check the source code and it does have
! str_contains($table, '\\')
at the beginning of a check. I tried looking for any logic about why if it does not have\\
it should be used directly (when we do have a! class_exists($table)
after this check) but I did not find anything.So, I followed these steps to accomplish the desired outcome:
src/Illuminate/Validation/Rules/DatabaseRule.php
tests/Validation/ValidationExistsRuleTest.php
and all of them passedI did have to do something super ugly I have never done, declare the whole test file inside a namespace (with
{ }
), because I had to declare a test model on the global namespace and I didn't want to add it on the main structure of the repository, nor insidetest
folder and declare a new namespace on the composer file (psr-4
forautoload-dev
).Do let me know if there is a better way of declaring this global namespace (
\
) test model class for this specific test.