简体   繁体   English

如何在laravel中更新数组值

[英]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: 这是我的userBasicInfo模型值:

        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? 我可以更新值,但问题是我只发送一个字段作为附属_facility的数组如何更新此值?

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: 您可以在我的身体请求中看到我发送“affiliated_facility”:[1]当我点击请求时它说:

   "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. 除非您在SQL中将该字段设置为JSON或类似字段,否则更改您的体系结构可能是最简单的解决方案。 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). affiliated_facility字段看起来应该是一个关系,而不是userBasicInfo模型中的单个字段,因为它可能是许多工具(因为数组可能会返回)。

If you set up a relation on the userBasicInfo model, something like: 如果在userBasicInfo模型上设置关系,则类似于:

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. 你可以使用list函数,但是你必须要小心,因为list函数可以改变PHP 4到5/7的行为。

For example: 例如:

You have array $array = [1,2,3] , so far fine right? 你有数组$array = [1,2,3] ,到目前为止还好吗?

So in PHP 4 if you do: 所以在PHP 4中如果你这样做:

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: 因为在PHP 4中, list函数从右到左分配值,但是如果你在7中使用输出它将是反向的,例如:

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? 它会对你的情况有所帮助,否则,我强烈建议你修改你的代码和结构,如果你的字段affiliated_facility不是一个数组那么为什么你收到一个呢? No makes sense right? 没有道理吗?

So before you try "approach" a solution in PHP for that I strongly suggest you revise your logic. 因此,在您尝试“接近”PHP的解决方案之前,我强烈建议您修改逻辑。

For more reference for the function, you can see at the documentation: https://www.php.net/manual/en/function.list.php 有关该功能的更多参考,请参阅文档: https//www.php.net/manual/en/function.list.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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