Laravel How do I return back with SQL errors displayed in HTML?

  laravel, laravel-8, mysql, php

Before downvoting this question please let me know how I can improve the question.

  • The problem?

I created a constraint in my SQL database to prevent duplicate entries. The initial laravel form submission works fine. I get back the correct message. When I try to throw a duplicate entry, the app, as expected, throws an error.

This is my Controller. A successful form submission does throw the correct error:


   return redirect('contactus.php')->with('status', 'We received your message. We will get back to you soon.');
   return back()->withErrors(['Your message may be a duplicate. Did you refresh the page? We blocked that submission. If you feel this was in error, e-mail us or call us.']);
  • Question

How do I display that error on the HTML screen? Instead of having the page display the following?

Error Thrown

Basically the contact form submits information into the database using laravel. When successful, it displays a success message by redirecting it. When not successful, (because of a SQL Unique constraint blocking duplicate entries) so far I’ve managed to make it throw a SQL error.

How do I display a custom message, like "post not successful, duplicate entry" in that case?

Thank you for your help

Source: Laravel

Leave a Reply