如何更新Laravel中的数据-数据中有数组值

zrfyljdw  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(143)

数组中有字段affiliated_facility,我正在尝试更新值:
我的控制器代码:

<?php
$affiliated_facility = implode(",", $userProfile['affiliated_facility']);
$userBasicInfoId = UserBasicInfo::where('user_id', $userProfile['id'])->value('id')->update([

    'work_phone' => $userProfile['work_phone'],
    'fax' => $userProfile['fax'],
    'extension' => $userProfile['extension'],
    'primary_facility' => $userProfile['primary_facility'],
    'employed_by' => $userProfile['employed_by'],
    'emergency_phone' => $userProfile['emergency_phone'],
    'designation' => $userProfile['designation'],
    'department' => $userProfile['department'],
    'employment_type' => $userProfile['employment_type'],
    'biography' => $userProfile['biography'],
    'hiring_date' => $userProfile['hiring_date'],
    'from' => $userProfile['from'],
    'to' => $userProfile['to'],
    'affiliated_facility' => $affiliated_facility]);   // this is in array so I used implode in top of this code

if ($userBasicInfoId) {
    $userBasicInfo = $this->userBasicInfo->find($userBasicInfoId);
    $userBasicInfo->fill($userProfile)->save();
} else {
    $userBasicInfo = $this->userBasicInfo->create($request->only($this->userBasicInfo->getModel()->fillable));
}
?>

但是当我点击我的请求时
对整数调用成员函数update()
我的代码中有一些错误。我想更新我的记录,但有一个数组中的字段:

"affiliated_facility": [1,2]

如何修改此代码?

ccrfmcuu

ccrfmcuu1#

UserBasicInfo::where('user_id', $userProfile['id'])是一个Builder示例。您应该获取Model示例来更新它。因此,请尝试:

UserBasicInfo::where('user_id', $userProfile['id'])->first()->update([...]);

相关问题