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

时间:2019-05-30 07:29:41

标签: php arrays laravel api

有一个数组中的字段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]

有人可以帮我修改此代码,我对此深感抱歉,我们将不胜感激!
预先谢谢你

1 个答案:

答案 0 :(得分:0)

UserBasicInfo::where('user_id', $userProfile['id']) Builder 实例。您应该获取 Model 实例进行更新。因此,尝试:

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