Ejemplo n.º 1
0
 public function update(Request $request, $id)
 {
     if ($this->readOnly) {
         return $this->response->build(['error' => 'method_not_allowed'], SymfonyResponse::HTTP_METHOD_NOT_ALLOWED);
     }
     $entity = $this->model->find($id);
     if (!$entity) {
         return $this->response->build(['error' => 'not_found'], SymfonyResponse::HTTP_NOT_FOUND);
     }
     $data = $request->all();
     // hook: beforeValidate()
     if ($hookResult = $this->beforeValidate($request, $data)) {
         return $hookResult;
     }
     // hook: beforeUpdateValidate()
     if ($hookResult = $this->beforeUpdateValidate($request, $id, $data)) {
         return $hookResult;
     }
     $validator = Validator::make($data, $this->model->getValidationRules($entity->id));
     if ($validator->fails()) {
         return $this->response->build(['error' => 'unprocessable_entity', 'validation_errors' => $validator->errors()->all()], SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY);
     }
     // hook: beforeUpdate()
     if ($hookResult = $this->beforeUpdate($request, $data, $id)) {
         return $hookResult;
     }
     $entity->fill($data)->save();
     $entity = $this->model->with($this->ctx->with())->find($id);
     // hook: afterUpdate()
     if ($hookResult = $this->afterUpdate($request, $entity)) {
         return $hookResult;
     }
     return $this->response->build($entity, SymfonyResponse::HTTP_OK);
 }