Пример #1
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $this->Form = new Form(route('manager::manager.api.users.update', [$id]), 'PUT');
     $this->Config();
     $collumns = $this->Model->getFillable();
     $primaryKey = $this->Model->getKeyName();
     $collectionRules = array();
     $collectionValues = array();
     foreach ($this->Form->GetFields() as $Field) {
         if (!is_null($Field->GetRules())) {
             $collectionRules[$Field->GetName()] = $Field->GetRules();
         }
     }
     $Validator = Validator::make($request->all(), $collectionRules);
     try {
         if (!$Validator->fails()) {
             foreach ($collumns as $collumn) {
                 if ($request->input($collumn) !== '') {
                     $collectionValues[$collumn] = $request->input($collumn);
                 } else {
                     $collectionValues[$collumn] = null;
                 }
             }
             try {
                 $data = $this->Model->where($primaryKey, $id)->update($collectionValues);
                 return $this->SetSuccessJsonResponseData($data);
             } catch (QueryException $e) {
                 return $this->SetErrorJsonResponseData($e);
             }
         } else {
             throw new ValidationException($Validator->messages());
         }
     } catch (ValidationException $e) {
         return $this->SetErrorJsonResponseData($e, HttpResponse::HTTP_BAD_REQUEST);
     }
 }