/
RegionsController.php
175 lines (137 loc) · 4.38 KB
/
RegionsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace Vinfo\Http\Controllers;
use Illuminate\Http\Request;
use Vinfo\Http\Requests\RegionsFormRequest;
use Vinfo\Http\Controllers\Controller;
use Vinfo\Region;
use Vinfo\RegionType;
use Vinfo\Country;
class RegionsController extends Controller
{
private function getDropdowns()
{
$countries = Country::withTranslation()->whereIsActive()->orderByTranslation('sortas')->get()->lists('name', 'id');
$region_types = RegionType::withTranslation()->orderByTranslation('sortas')->get()->lists('name', 'id');
view()->share('countries', $countries);
view()->share('region_types', $region_types);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$region = new Region;
$this->authorize('show', $region);
$regions = Region::with(['regionType', 'country'])
// ->where('regions.country_id', 765)
->orderByRelationTranslation('country', 'sortas')
->orderBy($region->getQualifiedOrderColumnName())
->paginate(25);
return view('regions.index', compact('regions'));
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
// public function countryIndex(Country $country)
// {
// $region = new Region;
// $this->authorize('show', $region);
// $regions = Region::withTranslation()
// ->with('regionType', 'country')
// ->orderBy($region->getQualifiedOrderColumnName())
// ->paginate(25);
// return view('regions.index', compact('regions'));
// }
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$region = new Region;
$this->authorize('create', $region);
$this->getDropdowns();
$mode = 'create';
return view('regions.create', compact('region', 'mode'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(RegionsFormRequest $request)
{
$region = new Region;
$this->authorize('create', $region);
if ($region->fill($request->input())->save()) {
return redirect(action('RegionsController@edit', $region->id))
->with('success', trans('messages.saved_success'));
}
return redirect(action('RegionsController@create'))
->withInput()
->with('error', trans('messages.saved_error'));
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$region = Region::findOrFail($id);
$this->authorize('show', $region);
return view('regions.show', compact('region'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$region = Region::whereid($id)->firstOrFail();
$this->authorize('update', $region);
$this->getDropdowns();
$mode = 'edit';
return view('regions.edit', compact('region', 'mode'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(RegionsFormRequest $request, $id)
{
$region = Region::findOrFail($id);
$this->authorize('update', $region);
$region->fill($request->input())->save();
return redirect(action('RegionsController@edit', $id))->with('success', trans('messages.saved_success'));
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$region = Region::findOrFail($id);
$this->authorize('destroy', $region);
if ($region->delete()) {
return redirect(action('RegionsController@index'))
->with('success', trans('messages.deleted_success'));
}
return redirect(action('RegionsController@edit', $region->id))
->with('error', trans('messages.deleted_error'));
}
}