如何在创建之前检查我的数据是否已经存在于我的数据库中?

How to check if my data already exists in my database before creating?

我正在为护士开发 Laravel 6 应用程序,我需要创建一个患者。我的问题是许多患者可以有相同的地址和姓氏。我尝试了不同的方法来检查患者是否已经存在,但结果是我不能让两个患者具有相同的数据。我想检查新患者的所有信息是否等于数据库中的一个。

我尝试了以下方法,但没有用。

if ($patientotal = Patient::where([
    ['nom', $data],
    ['prenom', $data],
    ['telephone', $data],
    ['adresse', $data],
    ['codePostale', $data],
    ['ville', $data],
    ['infoSupplementaire', $data]
])->doesntExist()) {
}

控制器

public function store(Request $request)
{
    $data = Validator::make($request->all(), [
        'nom' => 'required',
        'prenom' => 'required',
        'telephone' => 'required',
        'adresse' => 'required',
        'codePostale' => 'required',
        'ville' => 'required',
        'infoSupplementaire' => 'required',
    ]);

    $patient = new Patient();
    $patient->nom = $request->nom;
    $patient->prenom = $request->prenom;
    $patient->telephone = $request->telephone;
    $patient->adresse = $request->adresse;
    $patient->codePostale = $request->codePostale;
    $patient->ville = $request->ville;
    $patient->infoSupplementaire = $request->info;

    if ($patient->save()) {
        if ($request->has('debut', 'fin')) {
            $dataOrdo = Validator::make($request->all(), [
                'dateOrdonnance' => 'required',
            ]);
            $datedebut = Carbon::parse($request->debut);
            $datefin = Carbon::parse($request->fin);

            $ordonnance = new Ordonnance();
            $ordonnance->debut = $datedebut;
            $ordonnance->fin = $datefin;
            $ordonnance->patient_id = $patient->id;
            if ($ordonnance->save()) {
                session()->put('id_ordonnance', $ordonnance->id);

                return ['patient' => $patient, 'ordonnance' => $ordonnance];
            }
        }
    };

    return response()->json([$request->all()]);
}

我做到了,一个好人给我帮助,效果很好。非常感谢今年第一天的帮助

 public function store(Request $request)

{


        $data=  Validator::make($request->all(), [
            'nom'=>'required',
            'prenom'=>'required',
            'telephone'=>'required',
            'adresse'=>'required',
            'codePostale'=>'required',
            'ville'=>'required',
            'infoSupplementaire'=>'required',
        ]);
        $patientExists = Patient::where('nom', '=' ,$request->nom)
                     ->where('prenom', '=', $request->prenom)
                     ->where('telephone', '=', $request->telephone)
                     ->where('adresse', '=', $request->adresse)
                     ->where('codePostale', '=', $request->codePostale)
                     ->where('ville', '=', $request->ville)
                     ->where('infoSupplementaire', '=', $request->info)   
                     ->count();

                     if ($patientExists > 0) {

                        } else {

                            $patient = new Patient();
                            $patient->nom = $request->nom;
                            $patient->prenom = $request->prenom;
                            $patient->telephone = $request->telephone;
                            $patient->adresse = $request->adresse;
                            $patient->codePostale = $request->codePostale;
                            $patient->ville = $request->ville;
                            $patient->infoSupplementaire = $request->info;


                        if($patient->save()){


                            if($request->has('debut','fin')){


                             $dataOrdo=Validator::make($request->all(),[

                                 'dateOrdonnance'=>'required',

                             ]);
                                $datedebut = Carbon::parse($request->debut);
                                $datefin = Carbon::parse($request->fin);

                                $ordonnance=new Ordonnance();

                                $ordonnance->debut=$datedebut;
                                $ordonnance->fin=$datefin;
                                $ordonnance->patient_id=$patient->id;

                                if($ordonnance->save()){
                                 session()->put('id_ordonnance',$ordonnance->id);
                                 return['patient'=>$patient,'ordonnance'=>$ordonnance];

                                }

                            }
                        } ;



                             return response()->json([$request->all()]);
                        }

}