“SQLSTATE [23000]:完整性约束违规:1062重复条目

时间:2018-05-26 15:05:09

标签: jquery ajax laravel

我正在使用AJAX提交表单,并且在网络中我收到此错误。

"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'k@t.com' for key 'users_email_unique' (SQL: insert into `users` (`name`, `email`, `active`, `verify`, `user_type`, `password`) values (K, k@t.com, 2, , 3, $2y$10$FiwHZB1VKWo0skjrCn/16epQq5aqfwdQ055PZLebaK4XEyQFNuDai))"

我正在使用Laravel 5.6以及迁移。以下是此表的迁移。

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->tinyInteger('user_type');
        $table->tinyInteger('active');
        $table->rememberToken();
        $table->timestamps();
        $table->index('user_type');
        $table->index('active');
    });

而且,这是我的控制器的功能。

public function teacherApply(Request $request)
{

    if (Input::get()) {

        $data = Input::get();

        $email_check = User::where(['email' => $data['email'], 'active' => 1])->first();

        if (!empty($email_check))
        {
            return ["status" => 3, "message" => "Email already registered"];
        }
        else
        {

           $required_points = [

               'name'                   =>     'required',

               'firstName'              =>     'required',

               'lastName'               =>     'required',

               'email'                  =>     'required',

               'contact'                =>     'required',

               'password'               =>     'required',

               'degree'                 =>     'required',

               'field'                  =>     'required',

               'about'                  =>     'required'

           ];

        $validator = Validator::make($request->all(), $required_points);

        if ($validator->fails()) {

            return response()->json(['errors'=>$validator->errors(),'status' => 2]);

        }

        $name = $data['name'];

        $file_name    =    'Empty';

        $file         =    Input::file('image');

        if(!empty($file))
        {

        $destination   =    base_path('public/pmc_assets/teacher-samples');

        $file_name     =    upload_file($file, $destination);

        }

        $userID = User::insertGetId([

            'name'          =>     $name,

            'email'         =>     $data['email'],

            'active'        =>     2,

            'verify'        =>     '',

            'user_type'     =>     config('constant.teacher'), // Teacher

            'password'      =>     bcrypt($data['password']),
        ]);

        TeacherRequests::insert([

            'name'          =>     $data['firstName']." ".$data['lastName'],

            'phone'         =>     $data['contact'],

            'skype'         =>     $data['skype'],

            'user_id'       =>     $userID,

            'degree_type'   =>     $data['degree'],

            'degree_field'  =>     $data['field'],

            'about'         =>     $data['about'],

            'sample'        =>     $file_name,

            'resume'        =>     '',

            'active'        =>     2,

            'approved'      =>     0,

            'created_at'    =>     Carbon::now()

        ]);

        //send_to_flock_channel($name,"Manual","Teacher");

        return ["status"=> 1, "user_id" => $userID, "email" => $data['email']];

    }

    }

    return view('teacher.apply_teacher');
}

我的文件上传成功,数据也插入数据库,但是,我收到此错误。这不影响我,但想解决它?

1 个答案:

答案 0 :(得分:1)

在迁移文件中,$ table-> string(' email') - > unique(); 电子邮件ID具有唯一键,您可以删除唯一键

OR

你需要处理验证 的'电子邮件' =>'必需|唯一:用户' 而非'电子邮件' => '需'

OR

更改查询,如下所示 $ email_check = User :: where([' email' => $ data [' email']) - > first();