mpyw/unique-violation-detector
Detects and normalizes unique constraint violation errors across MySQL, PostgreSQL, and SQLite. Inspects PDO exceptions and SQLSTATE codes so your PHP/Laravel app can reliably catch duplicate-key errors and handle retries or upserts without brittle driver-specific checks.
SELECT then INSERT) fails under concurrency; the correct pattern is to let the database enforce uniqueness and catch the violation. Adopting this package is the enabling decision to move to the correct pattern without driver-specific code.UniqueConstraintViolationException (10.43+). This package only wins if you are outside that last case.Adopt when:
'Duplicate entry' is already spreading through the codebase.str_contains($e->getMessage(), ...) hacks into one auditable place.Look elsewhere when:
Illuminate\Database\UniqueConstraintViolationException natively and skip the dependency entirely.To executives: This is a small MIT-licensed utility, not a feature. It converts ambiguous database errors into a dependable signal, which lets us stop double-writing records under concurrency and show users a sensible message instead of a 500. The cost is one dependency; the risk is maintenance — minimal adoption, single maintainer. Because the code is tiny, we can neutralize that risk by vendoring the logic or by using the framework's built-in equivalent where our Laravel version allows.
To engineering: It replaces string matching and per-driver error-code conditionals with a single check. It removes a class of check-then-insert race bugs, makes queue retry decisions correct (unique violation means fail, not retry), and behaves consistently across MySQL, Postgres, SQLite, and SQL Server. Read the source before adopting — it is small enough to audit in an afternoon and to fork if upstream goes quiet. Recommended framing: adopt the pattern first, and treat the package as one implementation of it.
How can I help you explore Laravel packages today?