public function storePage($class, $id, $request) { // Modify some of the input data $this->modifyRequestData($request); $page = new Page(); // Return errors as JSON if request does not validate against model rules $v = Validator::make($request->all(), $page->rules()); if ($v->fails()) { return response()->json($v->errors(), 422); } $collection = $class::find($id); // Update the item with request data $page->fill($request->all()); // Check if the data saved OK if (!$collection->sections()->save($page)) { // Fail - Return error as JSON return response()->json(['errors' => [$this->messages['error_updating']]], 422); } else { // Success - Return item ID as JSON return response()->json($page, 200); } }