-1

i am trying to update data and my json has multiple objects currently my json is:

          "privileges":{

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

}

my controller:

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

but i find "Undefined index: id" error i dont where i am doing wrong your help will be highly appreciated!

syed1234
  • 761
  • 5
  • 12
  • 30

1 Answers1

0

you are accessing id object of facilities array you have to loop though facilities array or use index of array to get value id value of first index.

$fac = $this->userFacility->where('user_id', $facilities[0]['id'])->pluck('id')->first();
  • its not updating values but the error is gone – syed1234 Apr 05 '19 at 09:21
  • i dont understand why you put value('id') after where condition if you want just the id of that record you should first method and pluck that id . i have edited my ans you can check it. hope it'll help you! – Kamran AllahYar Apr 05 '19 at 10:35