middleware('auth'); $this->middleware('permission:county-list', ['only' => ['index']]); $this->middleware('permission:county-create', ['only' => ['create', 'store']]); $this->middleware('permission:county-edit', ['only' => ['edit', 'update']]); $this->middleware('permission:county-delete', ['only' => ['destroy', 'inactive', 'activate']]); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $counties = County::orderBy('name', 'asc')->get(); $districts = District::pluck('name', 'id'); return view('clinical_data::counties.index', compact('counties', 'districts')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $districts = District::pluck('name', 'id')->toArray(); $districts = ['' => '- select -'] + $districts; return view('clinical_data::counties.create', compact('districts')); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required', 'district_id' => 'required' ]); if ($validator->fails()) { $string = ""; foreach ($validator->errors()->getMessages() as $item) { $string .= "{$item[0]}
"; } flash($string)->error(); return back()->withErrors($validator)->withInput(); }else{ $user_id = Auth::user()->id; $county = new County; $county->name = $request->name; $county->district_id = $request->district_id; $county->created_by = $user_id; $county->updated_by = $user_id; try { $county->save(); flash($request->name . " County has been saved")->success(); return redirect("/counties/"); } catch (QueryException $e) { flash("An error occurred")->error(); return back()->withInput(); } } } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $county = County::where(['id' => $id])->first(); $districts = District::pluck('name', 'id')->toArray(); $districts = ['' => '- select -'] + $districts; if (!$county) { flash()->error("That county is not registered"); return redirect('/counties/'); } else { return view('clinical_data::counties.edit', compact('county', 'districts')); } } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { request()->validate([ 'name' => 'required', 'district_id' => 'required' ]); $county = County::find($id); $county->name = $request->name; $county->district_id = $request->district_id; $county->updated_by = Auth::user()->id; try { $county->save(); flash($request->name . " County has been updated")->success(); return redirect("/counties/"); } catch (QueryException $e) { flash("An error occurred")->error(); return back()->withInput(); } } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $county = County::find($id); if ($county->delete()) { flash("County has been deleted.")->success(); return redirect('/counties/'); } } /** * Display a listing of the inactive resource(s). * * @return \Illuminate\Http\Response */ public function inactive() { $counties = County::onlyTrashed() ->orderBy('name', 'asc') ->get(); $districts = District::pluck('name', 'id'); if (count($counties) < 1) { flash()->error("There is no inactive counties"); return redirect('/counties/'); } else { return view('clinical_data::counties.inactive', compact('counties', 'districts')); } } /** * Activate the specified resource in storage. * * @param int $id * @return \Illuminate\Http\Response */ public function activate($id) { $county = County::withTrashed()->find($id); if($county->restore()){ flash("County has been activated.")->success(); return redirect('/counties/inactive'); } } }