如何对抗laravel中的重复输入

Here I am facing an issue of duplicate entries in my database. I have setup a unique patient ID in my controller and I want to add users having the same e-mail, but a unique phone number.

What I have done and tried in my controller function:

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);
    }
}

Everything is working fine as per my requirement, but the code throws the following exception:

QueryException SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'front@gmail.com' for key 'patients_email_unique'

Here is my migration for the patients table:

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();
});

Change this line

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

To

$table->string('email');

You should add the unique validation rule to the email field like so:

$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',
]);
$this->validate($request,[
 'email' => 'required|email|unique',
]);

in your migration

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

You can use laravel validation for checking a unique in a specific table.

$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',
]);