Answers for "laravel duplicate database record"

PHP
3

laravel duplicate row

$data = Model::find(1);
$new_data = $data->replicate();
$new_data->created_at = now();
$new_data->save();
Posted by: Guest on July-03-2021
0

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 (Illuminate\Database\QueryException $e){
    $errorCode = $e->errorInfo[1];
    if($errorCode == 1062){
        // houston, we have a duplicate entry problem
    }
}
Posted by: Guest on March-09-2022

Code answers related to "laravel duplicate database record"

Browse Popular Code Answers by Language