duanliang789262 2019-05-30 07:29
浏览 165

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

There is field affiliated_facility which is in array and i am trying to update my values:

My controller code:

<?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));
}
?>

But when I hit my request it says

Call to a member function update() on integer
There is some mistakes in my code i want to update my record and there is one field coming which is in array

"affiliated_facility": [1,2]

can someone please help me to modify this code i am stuck on this your help will be highly appreciated!
Thankyou in advance

  • 写回答

1条回答 默认 最新

  • doujiu7704 2019-05-30 07:44
    关注

    UserBasicInfo::where('user_id', $userProfile['id']) is a Builder instance. you should get Model instance to update it. So, try:

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

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能