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\City\CityStoreRequest; |
10
|
|
|
use Turahe\Master\Http\Requests\City\CityUpdateUpdate; |
11
|
|
|
use Turahe\Master\Models\City; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class CityController. |
15
|
|
|
*/ |
16
|
|
|
class CityController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public function index(): View |
22
|
|
|
{ |
23
|
|
|
$data = City::with('provinces') |
24
|
|
|
->autoSort() |
25
|
|
|
->autoFilter() |
26
|
|
|
->search(request('search'))->paginate(); |
27
|
|
|
|
28
|
|
|
return view('master::cities.index', compact('data')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return View |
33
|
|
|
*/ |
34
|
|
|
public function create(): View |
35
|
|
|
{ |
36
|
|
|
return view('master::cities.create'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param CityStoreRequest $request |
41
|
|
|
* |
42
|
|
|
* @return RedirectResponse |
43
|
|
|
*/ |
44
|
|
|
public function store(CityStoreRequest $request): RedirectResponse |
45
|
|
|
{ |
46
|
|
|
City::create($request->validated()); |
47
|
|
|
|
48
|
|
|
return redirect() |
49
|
|
|
->route('master::cities.index') |
50
|
|
|
->with('success', 'City saved'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param City $city |
55
|
|
|
* |
56
|
|
|
* @return View |
57
|
|
|
*/ |
58
|
|
|
public function edit(City $city): View |
59
|
|
|
{ |
60
|
|
|
return view('master::cities.edit', compact('city')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param CityUpdateUpdate $request |
65
|
|
|
* @param City $city |
66
|
|
|
* |
67
|
|
|
* @return RedirectResponse |
68
|
|
|
*/ |
69
|
|
|
public function update(CityUpdateUpdate $request, City $city): RedirectResponse |
70
|
|
|
{ |
71
|
|
|
$city->update($request->validated()); |
72
|
|
|
|
73
|
|
|
return redirect() |
74
|
|
|
->route('master::cities.edit', $city) |
75
|
|
|
->with('success', 'City saved'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param City $city |
80
|
|
|
* |
81
|
|
|
* @throws \Exception |
82
|
|
|
* |
83
|
|
|
* @return RedirectResponse |
84
|
|
|
*/ |
85
|
|
|
public function destroy(City $city): RedirectResponse |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
|
|
$city->delete(); |
89
|
|
|
|
90
|
|
|
return redirect()->route('master::cities.index')->with('success', 'City deleted'); |
91
|
|
|
} catch (QueryException $e) { |
92
|
|
|
return redirect()->back()->with('error', $e->getMessage()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|