接收到SQLSTATE [23000]:违反完整性约束:1062重复输入错误

时间:2019-05-30 13:06:38

标签: php sql laravel

我不确定此错误来自何处。我不知道它将如何尝试将重复项添加到数据库中。有想法吗?我打错来源了吗?我将其范围缩小到我知道与签名部分有关的地方。我在数据库中只有一列ID的

public function hellosign(Request $request, $id) {
        $auth = $request->user("api");
        if(empty($auth)) return response()->json(['error'=>"No auth user found!"], 401);

        $recruitment = Recruitment::whereId($id)->first();
        if(empty($recruitment)){
            return response()->json(['error' => "Agent not found!"]);
        }

        $email = $recruitment->email;
        if(empty($email)) {
            return response()->json(['error' => "Please provide an email!"]);
        }

        $template = "onboarding_contract";

        Helper::graph("hellosign.create.".$template, 1);
        $client = new HelloSign\Client(config('hellosign.apikey'));
        $sign = new HelloSign\TemplateSignatureRequest;
        if(config('app.env') != "production") $sign->enableTestMode();
        $sign->setTitle("Onboarding Sales Team");
        $sign->setSubject('Please enter your information');
        $sign->setMessage('Please find below the Onboarding Sales Team information for review.');
        $sign->setTemplateId('****************');
        $sign->setSigner('Associate', $recruitment->email, $recruitment->first_name." ".$recruitment->last_name);
        $sign->setCustomFieldValue('rets_id_id_reference', "_".$id."_onboarding");
        $sign->setCustomFieldValue('template', $template);

        // Fields
        $json = [
            // Section 1
            'associate_name'     => $recruitment->name,
            'license'            => $recruitment->license,
            'start_at'           => $recruitment->start_at,
            'commission_agent'   => $recruitment->commission_agent * 100,
            'commission_other'   => $recruitment->commission_other * 100,
            'commission_lead'    => $recruitment->commission_lead * 100,
            // 'eo_coverage'        => "",
        ];

        foreach($json as $field => $value) {
            $sign->setCustomFieldValue($field, $value);
        }

        // Hellosign
        $sign_url = "";
        Trace::log('hellosign_create_onboarding', $json, $id, $auth->id);

        // Save query
        $signature = new Signature;
        $signature->uuid = "_".$id."_onboarding";
        $signature->template = $template;
        $signature->email = $recruitment->email;
        $signature->rets_id = "";
        $signature->id = $id;
        $signature->author_id = (Auth::check() ? Auth::user()->id : null);
        //$signature->reference = $reference;
        $signature->json = json_encode($json);
        $signature->save();

        // Send it
        $response = $client->sendTemplateSignatureRequest($sign);

        return response()->json(['ok']);
    }

1 个答案:

答案 0 :(得分:1)

删除$signature->id = $id;,让mysql自动添加唯一ID

$signature = new Signature;
$signature->uuid = "_".$id."_onboarding";
$signature->template = $template;
$signature->email = $recruitment->email;
$signature->rets_id = "";
$signature->author_id = (Auth::check() ? Auth::user()->id : null);
//$signature->reference = $reference;
$signature->json = json_encode($json);
$signature->save();