简体   繁体   中英

Call to a member function save() on array - using laravel

I am trying to update my user info and i have make object of json currently my json have object of user profile and i want to save my info using the json object i have also my controller code where i have replaced $request with $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
    }
    }

My controller where i am updating records:

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);
    }
}

i have replaced $request with $userProfile but i got an error how i can updated my records using making of json object. Your help will be highly appreciated!

Hello you can try this :

$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();
        }

Add This to your Code

> If I have any errors please edit it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM