php - 调用数组上的成员函数 save() - 使用 laravel
问题描述
我正在尝试更新我的用户信息,并且我已经制作了 json 的对象,目前我的 json 有用户配置文件的对象,我想使用 json 对象保存我的信息我还有我的控制器代码,其中我用 $userProfile 替换了 $request。
{
"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 对象的制作来更新我的记录。您的帮助将不胜感激!
解决方案
你好,你可以试试这个:
$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();
}
将此添加到您的代码中
> 如果我有任何错误,请编辑它
推荐阅读
- python-3.x - 在 jupyter 中找不到模块(folium)
- php - Laravel 中的 3 个表关系
- javascript - 发布请求不起作用,声称正文数据不存在
- python - 尝试使用 conda 安装 unrar 时出现 PackagesNotFoundError
- powershell - 部署组作业中的 Powershell 脚本步骤生成此错误:System.Management.Automation.Runspaces.Initializ 的类型初始化程序
- flutter - Dart 单例返回空值
- qt - 如何在 QML 中设置正确的路径到离线 OSM 目录(不使用 qrc)?
- angular - 将 Angular 库的资产文件复制到项目资产文件夹
- java - Spring Boot Web 应用程序的 JSP 页面在作为 war 包托管在外部 tomcat 中时未加载
- c# - Unity 项目无法在 Windows 和 IOS 等 Android 上运行