Example #1
0
 /**
  * Updates a model or creates a new one if `id` is null
  *
  * @param $request Request dependency injection of the request for doing the validation
  * @param $model
  * @param null $id when null creates a new row
  * @return $this|\Illuminate\Http\RedirectResponse
  */
 public function save(Request $request, $model, $id = null)
 {
     $class = static::getNamespaceForModel($model) . $model;
     $rules = null;
     if (method_exists($class, 'getValidationRules')) {
         $rules = $class::getValidationRules($id);
     }
     if ($rules) {
         $this->validate($request, $rules);
     }
     // Create if no id or update if there is ID
     try {
         if ($id == null) {
             $object = $class::create(setNullOnEmptyStrings(Input::all()));
         } else {
             $object = $class::find($id);
             $object->update(setNullOnEmptyStrings(Input::all()));
         }
     } catch (\Exception $e) {
         return redirect()->back()->withErrors(["update" => $e->getMessage()]);
     }
     return redirect()->back()->with(["message" => "Saved"]);
 }
Example #2
0
 /**
  * Update a resource
  *
  * @param Request $request
  * @param $id
  * @return $this|\Illuminate\Http\RedirectResponse
  */
 public function update(Request $request, $id)
 {
     $this->validate($request, $this->getValidationRules($id));
     try {
         if ($id) {
             $object = $this->getObjectFromRoute($id);
             $object->update(setNullOnEmptyStrings($request->all()));
         } else {
             $class = $this->getModelClassFromRoute();
             $class::create(setNullOnEmptyStrings($request->all()));
         }
     } catch (\Exception $e) {
         return redirect()->back()->withErrors(["update" => $e->getMessage()]);
     }
     return redirect()->back()->with(["message" => "Saved"]);
 }