2

在这里,我面临数据库中重复条目的问题。我在我的控制器中设置了一个唯一的患者 ID,我想添加具有相同电子邮件但唯一电话号码的用户。

我在控制器功能中所做并尝试过的事情:

public function store(Request $request) {    
    $validator = \Validator::make($request->all(), [
        'name' => 'required',
        'email' => 'required|email',
        'phone'=>'required|min:11|numeric|unique:patients',
        'birth_date' => 'required',
        'gender' => 'required',
        'address' => 'required',
    ]);

    if ($validator->fails()) {
        $errors = $validator->errors();
        return $errors->toJson();
    } else {
        $fileNameToStore = 'defaultimg/avatar.png';
        $post = new patient;
        $post->name = $request->input('name');
        $post->email = $request->input('email');
        $post->picture = $fileNameToStore;
        $post->birth_date = $request->input('birth_date');
        $post->gender = $request->input('gender');
        $address = htmlspecialchars($request->input('address'));
        $post->address = $address;
        $post->patient_id = 'PID' . rand(999, 9999999999999);
        $post->phone = $request->input('phone');
        $post->save();

        return response(array(
            'error' => false,
            'message' => 'Patient added successfully', 'patientid' => $post->patient_id,), 200);
    }
}

一切都按照我的要求工作正常,但代码抛出以下异常:

QueryException SQLSTATE [23000]:完整性约束违规:1062 重复条目 'front@gmail.com' 用于键 'patients_email_unique'

这是我对patients表的迁移:

Schema::create('patients', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('birth_date');
    $table->string('gender');
    $table->string('address');
    $table->string('patient_id');
    $table->string('picture');
    $table->string('phone')->unique();
    $table->rememberToken();
    $table->timestamps();
});
4

5 回答 5

5

您可以使用 laravel 验证来检查特定表中的唯一性。

$validator = \Validator::make($request->all(), [
    'name' => 'required',
    'email' => 'required|email|unique:patients,email',
    'phone'=>'required|min:11|numeric|unique:patients',
    'birth_date' => 'required',
    'gender' => 'required',
    'address' => 'required',
]);
于 2018-04-25T07:30:01.737 回答
2

您应该像这样将unique验证规则添加到email字段中:

$validator = \Validator::make($request->all(), [
    'name' => 'required',
    'email' => 'required|email|unique',
    'phone'=>'required|min:11|numeric|unique:patients',
    'birth_date' => 'required',
    'gender' => 'required',
    'address' => 'required',
]);
于 2018-04-25T07:01:39.253 回答
2
$this->validate($request,[
 'email' => 'required|email|unique',
]);

在您的迁移中

$table->string('email')->unique();
于 2018-04-25T07:20:21.027 回答
2

使用此代码。这里的输出显示true数据是否已经在数据库中找到

public function alreadyExistEmail(Request $request) {
        $patients = DB::table('patients')
        ->where('email',$request->email)
        ->first();

        if($patients === null )
            return response()->json('false', 200);
        else
            return response()->json('true', 200);
    }
于 2021-10-21T04:19:47.053 回答
1

更改此行

$table->string('email')->unique();

$table->string('email');
于 2018-04-25T07:03:06.023 回答