-
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
Laravel DB Transaction and Validation Order Issue #52414
Comments
To fully debug this, you should also place the code on the frontend. Either way, I think the transaction has nothing to do with the error showing on the frontend or not. If you're coding in Laravel 11, you should refer to the documentation of Laravel 11 to do things. There's an automatic redirection method, removes the necessity of the if you've placed to check if the validation has failed. Can you try this approach instead? |
Hi, @hugoboss17 Thank you for the response. I have already conducted extensive debugging on this issue, and unfortunately, the problem persists. The validation errors do not show up in the Blade view when I have tried the following:
Despite these efforts, the issue remains unresolved. The validation messages only display correctly when the transaction handling is adjusted as mentioned in my initial post. Could you please provide further insights or confirm if there have been changes to the database methods in Laravel 11 that might affect this behavior? Thank you. |
Heya, thanks for reporting. We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up. laravel new bug-report --github="--public" Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue. Thanks! |
Hello, @driesvints I've created the repository as requested. The code that reproduces the issue is committed on the main branch. Please find the repository here: GitHub Repository Link. Thank you for your assistance in resolving this issue! |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
Hello @Abhishek-Jetani and @driesvints ! I'm taking a look on this and I cannot reproduce the error on the repository example. It's working: Maybe I'm missing something. Thank you! |
Hello @alibori ! Thank you for checking the issue! Just to clarify, I have implemented this code in an existing working project, and I've also created a new project to test it. Additionally, two of my friends have implemented the same code in their Laravel 11 versions. Unfortunately, we're all experiencing the same issue, where the validation messages do not display in the Blade view file when Please let me know if there's any additional information I can provide. Thank you! |
Hello @Abhishek-Jetani @driesvints I have thoroughly debugged the issue and found that it is not related to the Laravel framework itself. In Laravel 10, the default SESSION_DRIVER in the .env file is set to file. This setup can cause an error when using DB::beginTransaction(), as the session errors are stored in files and subsequently displayed on the front end. However, in Laravel 11, the .env file defaults to SESSION_DRIVER=database, which prevents this issue from appearing on the front end. To replicate the issue from Laravel 10, you need to change SESSION_DRIVER=database to SESSION_DRIVER=file. This way, the error will be stored in the session and displayed on the front end as expected. When using SESSION_DRIVER=file, the error messages are stored and can be accessed using $errors. If SESSION_DRIVER=database is used, $errors will return an []empty array, but with file, it will return the correct error message in laravel 11. (Database driver) e.g., [] Here is the Laravel 11 code example: .env
web.php
App\Http\Controllers\TestController
resources\views\welcome.blade.php
Output Read More: HTTP SessionPlease let me know if you need more information about it. Thank you so much 😊 |
Hi @keyurbhimani ! In my test project the error message is shown whether the value of the @Abhishek-Jetani this solve your problem? |
Hi @Abhishek-Jetani ! When Thanks in advance! |
Hi @keyurbhimani , Thank you so much for the detailed explanation and code example! I implemented the changes you suggested by switching the SESSION_DRIVER to file, and it's working perfectly now. The validation messages are displaying as expected. I really appreciate your help with this! Thanks again 😊 |
Hello @alibori, Yes, the problem is resolved. After changing the SESSION_DRIVER value from database to file in the .env file, everything is working perfectly for me. Thank you for checking in! |
Hi @alibori, Thanks for the follow-up. I checked the logs, and when However, the validation fails as expected, and the $errors variable contains the following: So, the validation errors are correctly identified, but they aren't displayed in the Blade view as expected when using the database session driver. Here's the controller code I used for this test:
Let me know if you need any more information. Thanks again! |
Hi @Abhishek-Jetani ! To be honest I don't know why is this happening. Do you have the sessions table on your database, right? As far as I know the session's driver only defines where the session's data will be stored, so this should work with the database one too... Anyway, happy to hear that your problem seems to be solved! Regards |
The issue has been resolved. The problem was in the Thank you to everyone who helped! |
Laravel Version
11.15.0
PHP Version
8.2.0
Database Driver & Version
Mysql & 5.2.0
Description
In Laravel 10 and previous versions, the following code worked perfectly without any issues. However, in Laravel 11, I encountered a problem when using database transactions with validation.
Issue:
When I add
DB::beginTransaction();
above the validation code, the validation messages do not display in the Blade view file.Steps To Reproduce
Add
DB::beginTransaction();
before the validation logic.Perform validation.
If validation fails, the error messages do not show up in the Blade view file.
Non-working Code:
Solutions:
Solution 1:
Adding
DB::rollBack();
above the redirect statement with errors works.Solution 2:
Moving
DB::beginTransaction();
after handling validation errors works perfectly.Question:
Is this a bug in Laravel 11, or have there been any changes to the database methods that could cause this issue? This code works without any issues in Laravel 10 and previous versions.
Thank you for your time and assistance.
@taylorotwell @nunomaduro
The text was updated successfully, but these errors were encountered: