我正在尝试更新我的用户信息,并且我已经使json对象成为当前对象,而我的json具有用户个人资料对象,并且我想使用json对象保存我的信息,我也有我的控制器代码,其中我已将$ request替换为$ userProfile。
{
"user_profile": {
"email": "shahzadshahg@hotmail.com",
"password": "admin123",
"password_confirmation": "admin123",
"status": 0,
"first_name": "Shahzad",
"middle_name": "ali123",
"last_name": "Shah",
"date_of_birth": "2015-01-01",
"gender": "M",
"area_id": 1,
"address": "Minhatten NY",
"city": "New York",
"state": "Washington",
"zip": "12312",
"fax": "111-111-1111",
"phone_extension": "2471",
"work_phone": "111-111-1111",
"phone_no": "111-111-1111",
"emergency_contact": "111-111-1111",
"social_security": "111-11-1111",
"module_id": 2,
"role_id": 1,
"id": 1
}
}
我要更新记录的控制器:
public function update(Request $request) {
$body = $request->all();
$userProfile = $body['user_profile'];
$bodyObj = $userProfile;
$validator = UserValidations::validateUser($bodyObj, true);
if ($validator->fails()) {
return response(['status' => false,'message' => __('messages.validation_errors'), 'errors' => $validator->errors()->all()], 200);
}
DB::beginTransaction();
try{
$user = $this->user->find($userProfile['id']);
$user->fill($userProfile->all())->save();
$userProfile->request->add(['user_id' => $user->id]);
//Update User Basic Info
$userBasicInfoId = $this->userBasicInfo->where('user_id', $userProfile->input('id'))->value('id');
if($userBasicInfoId) {
$userBasicInfo = $this->userBasicInfo->find($userBasicInfoId);
$userBasicInfo->fill($userProfile->all())->save();
} else {
$userBasicInfo = $this->userBasicInfo->create($userProfile->only($this->userBasicInfo->getModel()->fillable));
}
//Update User Contact Details
$userContactDetailId = $this->userContactDetails->where('user_id', $userProfile->input('id'))->value('id');
if($userContactDetailId) {
$userContactDetails = $this->userContactDetails->find($userContactDetailId);
$userContactDetails->fill($userProfile->all())->save();
} else {
$userContactDetails = $this->userContactDetails->create($userProfile->only($this->userContactDetails->getModel()->fillable));
}
//Update User Module
$module_id = $this->userAccessModule->where('user_id', $userProfile->input('id'))->value('id');
if($module_id) {
$userModule = $this->userAccessModule->find($module_id);
$userModule->fill($userProfile->all())->save();
} else {
$userModule = $this->userAccessModule->create($userProfile->only($this->userAccessModule->getModel()->fillable));
}
//Update User Roles
$user_role_id = $this->userRoles->where('user_id', $userProfile->input('id'))->value('id');
if($user_role_id) {
$userRole = $this->userRoles->find($user_role_id);
$userRole->fill($userProfile->all())->save();
} else {
$userRole = $this->userRoles->create($userProfile->only($this->userRoles->getModel()->fillable));
}
//Getting the update User infor
$user = $this->user->with('userBasicInfo', 'userContactInfo','userAccessModule', 'userRole:id,user_id,role_id')->where('id', $request->input('id'))->first();
DB::commit();
return response(['status' => true, 'message' => 'User updated successfully', 'data' => $user], 200);
} catch(\Exception $ex) {
DB::rollback();
return response(['status' => false, 'message' => $ex->getMessage()], 500);
}
}
我用$ userProfile代替了$ request,但是出现一个错误,如何使用制作json对象来更新记录。 非常感谢您的帮助!
答案 0 :(得分:0)
您好,您可以尝试以下方法:
$json = file_get_contents(storage_path('user_profile.json'));
$users_profile = json_decode($json,true);
foreach ($users_profile as $user_profile) {
foreach ($$user_profile as $key => $value) {
$insertArr[str_slug($key,'_')] = $value;
$userBasicInfo->fill($user_profile->all())->save();
}
将此添加到您的代码中
>如果有任何错误,请对其进行编辑