Exact DB Error

If you want to catch Eloquent Query exceptions, use specific QueryException instead default Exception class, and you will be able to get the exact SQL code of the error.

1try {
2 // Some Eloquent/SQL statement
3} catch (\Illuminate\Database\QueryException $e) {
4 if ($e->getCode() === '23000') { // integrity constraint violation
5 return back()->withError('Invalid data');
6 }
7}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord