catching exceptions thrown by SQL INSERTs in CRUD Controllers #1015
Replies: 2 comments 5 replies
-
You should prevent that from happening by having validation in your Controller. In you create/update operations you should create something like a FormRequest with your rules for the fields. Most likely laravel See for example in our demo: https://github.com/Laravel-Backpack/demo/blob/main/app/Http/Requests/DummyRequest.php If you need to so something custom when that validation error trigger you should probably write your custom rule, that will behave similar to Laravel Cheers |
Beta Was this translation helpful? Give feedback.
-
Im glad you solve the problem. I will close the issue, but please feel free to re-open or create a new one if needed. Cheers. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a CRUD for a User model with SoftDeletes as a trait. email is a unique() column and when I'm trying to add a new user with an email of a soft deleted user I get a Illuminate\Database\UniqueConstraintViolationException with the following message:
This totally makes sense but I'd like to be able to
catch
when this exception is being thrown and do anupdate()
on the table when this happens but idk where I'd catch this exception.Any ideas?
For that matter I think it'd also be better if, for non soft deleted users, it just gave a more useful error, saying something like "A user with this email already exists" but idk where I'd do that in my CRUD controller either.
Beta Was this translation helpful? Give feedback.
All reactions