Laravel catch Eloquent “Unique” field error

Solution:

I’m assuming you use MySQL, it’s probably different for other systems

Okay first, the error code forĀ duplicate entry is 1062. And here’s how you retrieve the error code from the exception:

catch (Illuminate\Database\QueryException $e){
    $errorCode = $e->errorInfo[1];
    if($errorCode == 1062){
        // houston, we have a duplicate entry problem
    }
}