public function store(FormBuilder $formBuilder, Request $request) { $form = $formBuilder->create(StatesForm::class); // It will automatically use current request, get the rules, and do the validation if (!$form->isValid()) { return redirect()->back()->withErrors($form->getErrors())->withInput(); } States::create($request->all()); return redirect()->route('state.index'); }
public function addElement(request $request, $element) { switch ($element) { case 'tag': $validator = Validator::make($request->all(), ['title' => 'required|unique:tags']); if ($validator->fails()) { return redirect('admin/element/add/tag')->withErrors($validator); } Tag::create($request->only('title')); break; case 'city': $validator = Validator::make($request->all(), ['title' => 'required|unique:cities']); if ($validator->fails()) { return redirect('admin/element/add/city')->withErrors($validator); } Cities::create($request->only('title')); break; case 'state': $validator = Validator::make($request->all(), ['title' => 'required|unique:states']); if ($validator->fails()) { return redirect('admin/element/add/state')->withErrors($validator); } States::create($request->only('title')); break; case 'country': $validator = Validator::make($request->all(), ['title' => 'required|unique:countries']); if ($validator->fails()) { return redirect('admin/element/add/country')->withErrors($validator); } Countries::create($request->only('title')); break; } return redirect('admin/elements'); }