Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

how to catch duplicate entry to database in laravel

//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:
try{
  User::add_user($user_details);
}
catch (IlluminateDatabaseQueryException $e){
    $errorCode = $e->errorInfo[1];
    if($errorCode == 1062){
        // houston, we have a duplicate entry problem
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #catch #duplicate #entry #database #laravel
ADD COMMENT
Topic
Name
9+6 =