|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Turahe\Master\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\QueryException; |
|
6
|
|
|
use Illuminate\Http\RedirectResponse; |
|
7
|
|
|
use Illuminate\Routing\Controller; |
|
8
|
|
|
use Illuminate\View\View; |
|
9
|
|
|
use Turahe\Master\Http\Requests\District\DistrictStoreRequest; |
|
10
|
|
|
use Turahe\Master\Http\Requests\District\DistrictUpdateRequest; |
|
11
|
|
|
use Turahe\Master\Models\District; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class DistrictController. |
|
15
|
|
|
*/ |
|
16
|
|
|
class DistrictController extends Controller |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @return View |
|
20
|
|
|
*/ |
|
21
|
|
|
public function index(): View |
|
22
|
|
|
{ |
|
23
|
|
|
$districts = District::with('cities')->autoSort()->autoFilter()->search(request('search'))->paginate(); |
|
24
|
|
|
|
|
25
|
|
|
return view('master::districts.index', compact('districts')); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return View |
|
30
|
|
|
*/ |
|
31
|
|
|
public function create(): View |
|
32
|
|
|
{ |
|
33
|
|
|
return view('master::districts.create'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param DistrictStoreRequest $request |
|
38
|
|
|
* |
|
39
|
|
|
* @return RedirectResponse |
|
40
|
|
|
*/ |
|
41
|
|
|
public function store(DistrictStoreRequest $request): RedirectResponse |
|
42
|
|
|
{ |
|
43
|
|
|
District::create($request->validated()); |
|
44
|
|
|
|
|
45
|
|
|
return redirect()->route('master::districts.index')->with('success', 'District saved'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param District $district |
|
50
|
|
|
* |
|
51
|
|
|
* @return View |
|
52
|
|
|
*/ |
|
53
|
|
|
public function edit(District $district): View |
|
54
|
|
|
{ |
|
55
|
|
|
return view('master::districts.edit', compact('district')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param DistrictUpdateRequest $request |
|
60
|
|
|
* @param District $district |
|
61
|
|
|
* |
|
62
|
|
|
* @return RedirectResponse |
|
63
|
|
|
*/ |
|
64
|
|
|
public function update(DistrictUpdateRequest $request, District $district): RedirectResponse |
|
65
|
|
|
{ |
|
66
|
|
|
$district->update($request->validated()); |
|
67
|
|
|
|
|
68
|
|
|
return redirect()->route('master::districts.edit', $district)->with('success', 'District saved'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param District $district |
|
73
|
|
|
* |
|
74
|
|
|
* @throws \Exception |
|
75
|
|
|
* |
|
76
|
|
|
* @return RedirectResponse |
|
77
|
|
|
*/ |
|
78
|
|
|
public function destroy(District $district): RedirectResponse |
|
79
|
|
|
{ |
|
80
|
|
|
try { |
|
81
|
|
|
$district->delete(); |
|
82
|
|
|
|
|
83
|
|
|
return redirect()->route('master::districts.index')->with('success', 'District deleted'); |
|
84
|
|
|
} catch (QueryException $e) { |
|
85
|
|
|
return redirect()->back()->with('error', $e->getMessage()); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|