district = $district; $this->province = $province; } /** * Display a listing of the resource. */ public function index(Request $request) { $data['title'] = 'District List'; if ($request->ajax()) { $model = District::query(); return DataTables::eloquent($model) ->setRowId('{{$id}}') ->setRowClass('{{"text-center align-middle"}}') ->addColumn('province', function(District $district){ return $district->province?->name; }) ->addColumn('action', 'admin::districts.datatables.action-btn') ->rawColumns(['province','action']) ->toJson(); } return view('admin::districts.index', $data); } /** * Show the form for creating a new resource. */ public function create() { $data['title'] = 'Create District'; $data['editable'] = false; $data['provinceLists'] = $this->province->pluck(); return view('admin::districts.create', $data); } /** * Store a newly created resource in storage. */ public function store(Request $request): RedirectResponse { try { $this->district->create($request->all()); return redirect()->route('district.index')->with('sucess', 'District has been created!'); } catch (\Throwable $th) { return redirect()->back()->withError($th->getMessage()); } } /** * Show the specified resource. */ public function show($id) { return view('admin::districts.show'); } /** * Show the form for editing the specified resource. */ public function edit($id) { $data['title'] = 'Edit District'; $data['editable'] = true; $data['district'] = $this->district->getDistrictById($id); $data['provinceLists'] = $this->province->pluck(); return view('admin::districts.edit', $data); } /** * Update the specified resource in storage. */ public function update(Request $request, $id) { try { $this->district->update($id, $request->except(['_method', '_token']) ); return redirect()->route('district.index')->with('success','District has been updated!'); } catch (\Throwable $th) { return redirect()->back()->withError($th->getMessage()); } } /** * Remove the specified resource from storage. */ public function destroy($id) { try { $this->district->delete($id); return response()->json(['status' => 200, 'message' => 'District has been deleted!'], 200); } catch (\Throwable $th) { return response()->json(['status' => 500, 'message' => 'District to delete!', 'error' => $th->getMessage()], 500); } } }