Try catch for laravel is not working for duplicate entry
<p>I am using below code in laravel controller. And getting duplicate error for <code>username</code> but I need to handle it by try-catch. This code is not working.</p>

<pre><code>&lt;?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Response;
use DB;

class StaffController extends Controller
{
    public function saveMember(Request $request){
        $errormsg = "";
        $result = false;
        try{
            $result = DB::table('members')-&gt;insert(
                    [
                        'username' =&gt; $request-&gt;username,
                        'phone' =&gt; $request-&gt;phone,
                        'status' =&gt; 1
                    ]
                );
        }catch(Exception $exception)
        {
            $errormsg = 'Database error! ' . $exception-&gt;getCode();
        }
        return Response::json(['success'=&gt;$result,'errormsg'=&gt;$errormsg]);
    }
}
</code></pre>

<p>I am getting this error, which I need to handle by try and catch </p>

<pre><code>SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'test1' for key 'username' 
</code></pre>

<p>Thanks for your help.</p>