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

问题描述 投票:0回答:1

数组中有一个附属设施字段,我正在尝试更新我的值:

我的控制器代码:

<?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]

如何修改此代码?

php arrays laravel
1个回答
0
投票

UserBasicInfo::where('user_id', $userProfile['id'])
是一个 Builder 实例。您应该获取 Model 实例来更新它。所以,尝试一下:

 UserBasicInfo::where('user_id', $userProfile['id'])->first()->update([...]);
© www.soinside.com 2019 - 2024. All rights reserved.