Php 根据用户id检查记录是否已经存在,如果存在则更新,如果不存在则插入-使用laravel

Php 根据用户id检查记录是否已经存在,如果存在则更新,如果不存在则插入-使用laravel,php,laravel,authentication,Php,Laravel,Authentication,我正在尝试根据用户id更新记录(如果该记录已存在),如果不存在,则根据laravel中的用户id插入新记录: 但是我的用户id在$doctorProfile['user\u id'内 数据正在插入,但用户id插入为空: 我的控制器代码: if($request->has('doctor_profile')) { $doctorProfile = $body['doctor_profile'];

我正在尝试根据用户id更新记录(如果该记录已存在),如果不存在,则根据laravel中的用户id插入新记录:

但是我的用户id在$doctorProfile['user\u id'内 数据正在插入,但用户id插入为空:

我的控制器代码:

          if($request->has('doctor_profile'))
        {
            $doctorProfile = $body['doctor_profile'];


                 $data_array = [

                'user_id' => $doctorProfile['user_id'],
                'medical_credentials' => 
               $doctorProfile['medical_credentials'],
                'registration_number' => 
           $doctorProfile['registration_number'],
                'registration_expiration_date' => 
          $doctorProfile['registration_expiration_date'],
                'dea_number' => $doctorProfile['dea_number'],
                'dea_expiration_date' => 
       $doctorProfile['dea_expiration_date'],
                'dea_issue_date' => $doctorProfile['dea_issue_date'],
                'npi_number' => $doctorProfile['npi_number'],
                'billing_title' => $doctorProfile['billing_title'],
                'billing_employment_type' => 
       $doctorProfile['billing_employment_type'],
                'other_employment_type' => $doctorProfile['other_employment_type'],
                'nadean_number' => $doctorProfile['nadean_number'],
                'upin' => $doctorProfile['upin'],
                'wcb_authorization' => $doctorProfile['wcb_authorization'],
                'wcb_rating_code' => $doctorProfile['wcb_rating_code'],
                'wcb_date_of_issue' => $doctorProfile['wcb_date_of_issue'],
                'hospital_privileges' => $doctorProfile['hospital_privileges'],
            ];

            $medId = MedicalIdentifiers::firstOrCreate(['user_id' => $doctorProfile['user_id']], $data_array);
            $medId->save();

        }
你可以让它更容易

 if($request->has('doctor_profile'))
     MedicalIdentifiers::firstOrCreate($request->all());
firstOrCreate()
在查找匹配项之前,检查所有要存在的参数。如果不是所有参数都匹配,那么将创建模型的新实例

如果只想检查特定字段,则使用
first或create(['field\u name'=>'value'])
在数组中仅使用一项。这将返回第一个匹配的项,如果没有找到匹配项,则创建一个新项

firstOrCreate()
firstOrNew()
之间的区别:

  • firstOrCreate()
    将自动在 如果没有找到匹配项,则返回数据库。否则它会给你 匹配项
  • firstOrNew()
    将为您提供一个新的模型实例 未找到匹配项,但仅在 您可以显式地这样做(在模型上调用save()。否则它 我会给你匹配的物品
选择一个或另一个取决于你想做什么。如果要在首次保存模型实例之前对其进行修改(例如,设置名称或某些必填字段),应使用
firstOrNew()
。如果只需使用参数即可在数据库中立即创建新模型实例,而无需对其进行修改,则可以使用
first或create(

from:

您可以让它变得更简单

 if($request->has('doctor_profile'))
     MedicalIdentifiers::firstOrCreate($request->all());
firstOrCreate()
在查找匹配项之前,检查所有要存在的参数。如果不是所有参数都匹配,那么将创建模型的新实例

如果只想检查特定字段,则使用
first或create(['field\u name'=>'value'])
在数组中仅使用一项。这将返回第一个匹配的项,如果没有找到匹配项,则创建一个新项

firstOrCreate()
firstOrNew()
之间的区别:

  • firstOrCreate()
    将自动在 如果没有找到匹配项,则返回数据库。否则它会给你 匹配项
  • firstOrNew()
    将为您提供一个新的模型实例 未找到匹配项,但仅在 您可以显式地这样做(在模型上调用save()。否则它 我会给你匹配的物品
选择一个或另一个取决于你想做什么。如果要在首次保存模型实例之前对其进行修改(例如,设置名称或某些必填字段),应使用
firstOrNew()
。如果只需使用参数即可在数据库中立即创建新模型实例,而无需对其进行修改,则可以使用
first或create(


from:

如果在调用first或create之前转储$data\u数组,会得到什么结果?像dd($data_数组);?您能告诉我们您在哪里声明
$body
变量的数据吗?您能告诉我们该请求是如何构造的吗?如果在调用First或Create之前转储$data_数组,会得到什么结果?像dd($data_数组);?您能告诉我们您在哪里声明
$body
变量的数据吗?你能告诉我们这个请求是如何组织的吗?