2

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:

$contact->save();

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?

1 Answer 1

2

You can do it by using try catch and query exception:

try {
    $contact->save();
    return redirect('contactus.php')->with('status', 'We received your message. We will get back to you soon.');

} catch(\Illuminate\Database\QueryException $e){
    $errorCode = $e->errorInfo[1];
    if($errorCode == '1062'){
       return back()->with('error', '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.');
    }
    else{
     return back()->with('error', $e->getMessage());
    }
}

or another way you can find/check the data first, if already exist just send the error. example:

$contact = Contact::where('email',$request->email)->first();
if($contact)
{
   return back()->with('error', '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.');
}

dont forget to get the error on the form view using like below:

<script>
  @if(session()->has('error'))
    alert('{{session()->get('error')}}')
  @endif
 </script>
Sign up to request clarification or add additional context in comments.

4 Comments

The code displayed "We received your message. We will get back to you soon." But it did not display "Your message may be a duplicate. Did you refresh the page?" Could you please tell me how to display the second message?
I updated the answer, it's better to use error handling like that for your case.
I found my own solution. I added this code where I want it to display the error. @if($errors->any()) <h2>{{$errors->first()}}</h2> @endif
Could you please upvote my question? This system is preventing me from asking more questions unless it's upvoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.