public function edit(Request $request) { $id = 0; if (isset($request->id)) { $id = $request->id; } $modelData = new \stdClass(); if ($id > 0) { try { $modelData = CarBrand::findOrFail($id); } catch (ModelNotFoundException $e) { session()->flash('message', trans('admin_common.Invalid Car Brand')); return redirect(url('admin/carbrand')); } } /** * form is submitted check values and save if needed */ if ($request->isMethod('post')) { /** * validate data */ $rules = ['car_brand_name' => 'required|max:255']; $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { $this->throwValidationException($request, $validator); } /** * get data from form */ $data = $request->all(); if (isset($data['car_brand_active'])) { $data['car_brand_active'] = 1; } else { $data['car_brand_active'] = 0; } /** * save or update */ if (!isset($modelData->car_brand_id)) { CarBrand::create($data); } else { $modelData->update($data); } /** * clear cache, set message, redirect to list */ Cache::flush(); session()->flash('message', trans('admin_common.Car Brand saved')); return redirect(url('admin/carbrand')); } return view('admin.car_brand.car_brand_edit', ['modelData' => $modelData]); }