'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": "[email protected]",
"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" (marked with [1]
above). 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 can I update this specific field which I am sending as array?
Solution 1:[1]
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', []));
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Watercayman |
Solution 2 | Fabio William Conceição |