简体   繁体   中英

How to update array value in laravel

My controller code:

        $userBasicInfoId = $this->userBasicInfo->where('user_id', $userProfile['id'])->value('id');
        if($userBasicInfoId) {
            $userBasicInfo = $this->userBasicInfo->find($userBasicInfoId);
            $userBasicInfo->fill($userProfile)->save();
        } else {
            $userBasicInfo = $this->userBasicInfo->create($request->only($this->userBasicInfo->getModel()->fillable));
        }

This is my userBasicInfo model values:

        protected $table = "user_basic_info";

protected $fillable = [
    'first_name', 'middle_name', 'last_name', 'profile_pic', 'date_of_birth', 'gender', 'area_id', 'user_id', 'created_by', 'updated_by', 'created_at', 'deletedAt','title','cell_no','address','ssn','work_phone','fax','extension','primary_facility','affiliated_facility','employed_by','emergency_phone','designation','department','employment_type','biography','hiring_date','from','to'
];

i can update the values but the issue is there is only one field which i am sending as array that is affiliated_facility how i can update this value?

My body request:

                  "user_profile": {

    "id": 38,
    "email": "shahzad124@ovadamd.com",
    "status": 0,
    "first_name": "shahzad12",
    "middle_name": "Admin",
    "last_name": "super",
    "date_of_birth": "2015-01-01",
    "gender": "M",
    "address": "Minhatten NY",
    "city": "New York",
    "state": "Washington",
    "zip": "12312",
    "fax": "111-111-1111",
    "extension": "2471",
    "work_phone": "111-111-1111",
    "social_security": "111-11-1111",
    "module_id": 2,
    "title": "Mr",
    "cell_no": "124788",
    "ssn": "256",
    "primary_facility": 1,
    "affiliated_facility":  [1],
    "employed_by": "john",
    "emergency_phone": "57744",
    "designation": " supervisor",
    "department": "facility",
    "employment_type": "Temporary",
    "biography": "I am Doctor",
    "hiring_date": "2015-01-01",
    "from": "2015-01-01",
    "to": "2015-01-01",
    "image": "" 
    },

You can see in my body request i am sending "affiliated_facility": [1] when i hit request it says:

   "Array to string conversion (SQL: update `user_basic_info` set `gender` = M, `updated_at` = 2019-05-29 18:19:34, `affiliated_facility` = 1 where `id` = 36)"

How i can update this specific field which i am sending as array?

Your help will be highly appreciated Thanks in advance

Unless you have that field set up as a JSON or similar in SQL, changing your architecture may be the easiest solution. That affiliated_facility field looks like it should be a relation, not a single field on your userBasicInfo model, since it could be many facilities (since an array may return).

If you set up a relation on the userBasicInfo model, something like:

public function affiliatedFacilities(){
    return $this->belongsToMany("App\AffiliatedFacility");
}

Then when you update, you can attach / sync automatically:

$userBasicInfo->affiliatedFacilities()->sync($request->get('affiliated_facility', []));

You can use list function, but you have to beware of that because the list function could change the behaviors for PHP 4 to 5/7.

For example:

You have array $array = [1,2,3] , so far fine right?

So in PHP 4 if you do:

list($one, $two, $three) = $array

Your output will be:

$one = 3, $two = 2, $three = 1

Because in the PHP 4 the list function assign the values from right to left, but if you using in 7 the output it'll be reverse, example:

list($one, $two, $three) = $array
// $one = 1, $two = 2, $three = 4

It'll gonna help for your case, otherwise, I strongly suggest you revise your code and structure, if your field affiliated_facility isn't an array so why you're receiving one? No makes sense right?

So before you try "approach" a solution in PHP for that I strongly suggest you revise your logic.

For more reference for the function, you can see at the documentation: https://www.php.net/manual/en/function.list.php

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