我的控制器代码:
$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));
}
这是我的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'
];
我可以更新值,但是问题是我只有一个字段作为affiliated_facility作为数组发送,我如何更新此值?
我的身体要求:
"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": ""
},
您可以在我的身体请求中看到我正在发送“ 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)"
我如何更新作为数组发送的此特定字段?
我们将非常感谢您的帮助 预先感谢
答案 0 :(得分:0)
除非您将该字段设置为JSON或SQL中的类似字段,否则更改体系结构可能是最简单的解决方案。 class A:
def f(self):
print("A")
class B:
pass
class C(B, A):
def g(self):
super().f() # should be just self.f()
print("C")
C().g() # prints A C
字段看起来应该是一个关系,而不是您的 userBasicInfo 模型上的单个字段,因为它可能有很多功能(因为数组可能返回)。
如果您在 userBasicInfo 模型上建立关系,则类似:
affiliated_facility
然后,当您更新时,您可以自动附加/同步:
public function affiliatedFacilities(){
return $this->belongsToMany("App\AffiliatedFacility");
}
答案 1 :(得分:0)
您可以使用list
函数,但是必须注意,因为list
函数可能会将PHP 4的行为更改为5/7。
例如:
您有数组$array = [1,2,3]
,到目前为止还好吗?
因此在PHP 4中,如果这样做:
list($one, $two, $three) = $array
您的输出将是:
$one = 3, $two = 2, $three = 1
因为在PHP 4中,list
函数从右到左分配值,但是如果在7中使用输出,它将是相反的,例如:
list($one, $two, $three) = $array
// $one = 1, $two = 2, $three = 4
这将对您的情况有所帮助,否则,如果您的字段affiliated_facility
不是数组,那么我强烈建议您修改代码和结构,为什么要收到一个?没有道理吧?
因此,在尝试使用PHP解决方案之前,我强烈建议您修改逻辑。
有关该功能的更多参考,请参见文档:https://www.php.net/manual/en/function.list.php