如何使用JSON对象更新数据-使用Laravel

时间:2019-04-05 07:50:45

标签: php json laravel

我正在尝试更新数据,并且我的json当前有多个对象,我的json是:

          "privileges":{

          "facilities": [
      {
        "facility_id": 1,
        "is_facility_supervisor": 0,
        "speciality_id": 2,
        "id": 1
      }
    ]

}

我的控制器:

        $body = $request->all();

         $userPrev = $body['privileges'];
    $facilities = $userPrev['facilities'];

          $fac = $this->userFacility->where('user_id', $facilities['id'])->value('id');

        if($fac) {
            $userFac = $this->userFacility->find($fac);
            $userFac->fill($facilities)->save();
        } else {
            $userFac = $this->userFacility->create($request->only($this->userFacility->getModel()->fillable));
        }

但是我发现“ Undefined index:id”错误,我不在哪里做错 非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您正在访问设施数组的id对象,您必须遍历设施数组或使用数组的索引来获取第一个索引的值id值。

$fac = $this->userFacility->where('user_id', $facilities[0]['id'])->pluck('id')->first();