/** * Update the specified resource in storage. * * @param int $id * @return PaulVL\JsonApi\Response */ public function update(Request $request, $id) { $class = $this->model_class; $response = new Response(); $object = $class::findOrFail($id); $inputs = ArrayHelper::empty_to_null($request->all()); $validator = Validator::make($inputs, $object->getUpdateRules()); if ($validator->fails()) { $validation_errors = StringHelper::concatInOneLine($validator->errors()->all(), ' '); return $response->responseUnprocessableEntity($validation_errors); } if (method_exists($this, 'updateData')) { return $this->updateData($object, $inputs); } else { try { $updated_rows = $class::where($object->getKeyName(), $object->getKey())->update($inputs); $object = $class::findOrFail($id); $response->handleData($object); return $response->response(); } catch (Exception $e) { return $response->responseInternalServerError(); } } }
/** * Replace the namespace in the stub. * * @param string $stub * @return $this */ protected function replaceNamespace(&$stub) { $name = '\\' . $this->argument('name'); $className = ucwords(str_replace('\\', '', strrchr('\\' . $this->argument('name'), '\\'))); $name = StringHelper::str_lreplace('\\', '', str_replace($className, '', $name)); $namespace = $this->getAppNamespace() . 'Http\\Controllers' . $name; $stub = str_replace('{{namespace}}', $namespace, $stub); return $this; }
public function getUpdateRules() { $update_rules = static::$rules; foreach ($update_rules as $key => $value) { if (StringHelper::contains($value, "unique")) { $update_rules[$key] = $value . "," . $this->getKey() . "," . $this->getKeyName(); } } return $update_rules; }