当我使用 maatwebsite 通过 csv 输入数据时,如何在 laravel 中复制验证

How can i duplicate validation in laravel when i input data by csv using maatwebsite

下面是插入数据库的代码 我使用的数据库字段sreg_no是唯一的。 SQLSTATE [23000]:违反完整性约束:1062 键 'administrations_sreg_no_unique'

的重复条目“1312764483”
    public function storestudent(Request $request){
        $request->validate([
            'file' =>'required|file|mimes:csv,txt|max:2048',
        ]);
        if ($request->hasFile('file')) {
            $path=$request->file('file')->getRealPath();
            $data=Excel::load($path)->get();
            foreach ($data as  $row) {


                Administration::create([
                    'sname'=>$row->sname,
                    'sfname'=>$row->sfname,
                    'smname'=>$row->smname,
                    'student_mobile'=>$row->student_mobile,
                    'admission_roll'=>$row->admission_roll,
                    'sboard_name'=>$row->sboard_name,
                    'spass_year'=>$row->spass_year,
                    'sroll_no'=>$row->sroll_no,
                    'sreg_no'=>$row->sreg_no,
                    'sgroup'=>$row->sgroup,
                    'sgpa'=>$row->sgpa,
                    'hboard_name'=>$row->hboard_name,
                    'hpass_year'=>$row->hpass_year,
                    'hroll_no'=>$row->hroll_no,
                    'hreg_no'=>$row->hreg_no,
                    'hgroup'=>$row->hgroup,
                    'hgpa'=>$row->hgpa,
                    'm_position'=>$row->m_position,
                    'session'=>$row->session,
                    'faculty'=>$row->faculty,
                    'student_password'=>$row->student_password,
                    'gender'=>$row->gender
                ]);

                //Notify Student

            }
        }
        session()->flash('success', 'Student Inserted  Successfully !!');
        return redirect()->back();
    }

其实我想通知输入数据的用户。这是重复数据。谢谢

在尝试创建新的 Administration 记录之前检查数据是否包含重复的数字:

if (Administration::where('sreg_no', $row->sreg_no)->exists()) {
    session()->flash('error', 'Duplicate Student Registration Number');
    return redirect()->back();
} else {
    // create new administration record, notify the student, and return redirect back
}