title: How to handle QueryException for duplicate entries in Laravel
body: <p>Here is my model code trying to handle <code>duplicate entries</code>:</p>
  
  <pre><code>$userData = ['name' =&gt; $name, 'email' =&gt; $email, 'password' =&gt; $password]; 
  
     public function addUser($userData) {     
          try {
              DB::table('users')-&gt;insert($userData);
          } catch (QueryException $e) {
              $errorCode = $e-&gt;errorInfo[1];          
              if($errorCode == 1062){
                  throw ('Duplicate Entry');
              }
          }
      }
  </code></pre>
  
  <p>Calling controller code looks like: <code>$userModel-&gt;addUser($userData);</code></p>
  
  <p>Here I am not trying to print any response received from <code>model</code>.</p>
  
  <p><strong>Getting error as:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/wUnm6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wUnm6.png" alt="enter image description here"></a></p>
  
  <p>What am I doing wrong? How to handle Exceptions correctly and what's the best practise of doing it? </p>