【问题标题】:How to update data in laravel - there is array value in data如何更新laravel中的数据-数据中有数组值
【发布时间】:2019-10-15 20:08:09
【问题描述】:

有一个字段位于数组中,我正在尝试更新我的值:

我的控制器代码:

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

有人可以帮我修改这段代码吗? 提前谢谢你

【问题讨论】:

  • 我认为问题出在-&gt;value('id')-&gt;update - 为什么-&gt;value('id') 是为了?
  • 根据“id”更新记录,这个id是主键
  • 但这就是where('user_id', $userProfile['id']) 的作用
  • 如果我删除 ->value('id) 然后它说 Call to a member function fill() on null
  • @syed1234 可能是 $userProfile['id'] 在表中不存在或为空,

标签: php arrays laravel api


【解决方案1】:

UserBasicInfo::where('user_id', $userProfile['id']) 是一个 Builder 实例。你应该得到 Model 实例来更新它。所以,试试:

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

【讨论】:

  • 我试过了,但它说 Call to a member function fill() on null
  • 然后向我们展示您在尝试 dd(UserBasicInfo::where('user_id', $userProfile['id'])->first()) 时看到的内容
猜你喜欢
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 2021-12-24
  • 2020-03-27
  • 2018-11-28
  • 2017-06-08
  • 2018-06-11
  • 1970-01-01
相关资源
最近更新 更多