| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 71.43% covered (warning) | 71.43% | 5 / 7 | CRAP | 94.44% covered (success) | 94.44% | 34 / 36
CitiesController | 0.00% covered (danger) | 0.00% | 0 / 1 | 71.43% covered (warning) | 71.43% | 5 / 7 | 15.04 | 94.44% covered (success) | 94.44% | 34 / 36
index | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 3 / 3
create | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 3 / 3
store | 0.00% covered (danger) | 0.00% | 0 / 1 | 3.03 | 85.71% covered (warning) | 85.71% | 6 / 7
edit | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 4 / 4
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 3.01 | 88.89% covered (warning) | 88.89% | 8 / 9
destroy | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 5 / 5
validatorCities | 100.00% covered (success) | 100.00% | 1 / 1 | 1 | 100.00% covered (success) | 100.00% | 5 / 5

1 | <?php
2 |
3 | namespace App\Http\Controllers;
4 |
5 | use App\City;
6 | use App\Province;
7 | use Illuminate\Http\Request;
8 | use Illuminate\Support\Facades\Auth;
9 | use Illuminate\Support\Facades\Gate;
10 | use Illuminate\Support\Facades\Validator;
11 |
12 | class CitiesController extends Controller
13 | {
14 | public function index()
15 | {
16 | if(!Gate::allows('城市-查询')){ return redirect(url('/')); }
17 | $cities=City::paginate(10);
18 | return view('maintenance.city.index',['cities'=>$cities]);
19 | }
20 |
21 | public function create()
22 | {
23 | if(!Gate::allows('城市-录入')){ return redirect(url('/')); }
24 | $provinces=Province::get();
25 | return view('maintenance.city.create',['provinces'=>$provinces]);
26 | }
27 |
28 |
29 | public function store(Request $request)
30 | {
31 | if(!Gate::allows('城市-录入')){ return redirect(url('/')); }
32 | $this->validatorCities($request)->validate();
33 | $cities=$request->input('City');
34 | if(City::create($cities)){
35 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
36 | return redirect('maintenance/city')->with('successTip','新城市“'.$request->input('City.name').'”添加成功');
37 | };
38 | }
39 |
40 | public function edit($id)
41 | {
42 | if(!Gate::allows('城市-编辑')){ return redirect(url('/')); }
43 | $city=City::find($id);
44 | $provinces=Province::get();
45 | return view('maintenance.city.edit',['city'=>$city,'provinces'=>$provinces]);
46 | }
47 |
48 | public function update(Request $request, $id)
49 | {
50 | if(!Gate::allows('城市-编辑')){ return redirect(url('/')); }
51 | $this->validatorCities($request)->validate();
52 | $data=$request->input('City');
53 | $city=City::find($id);
54 | $city->fill($data);
55 | if ($city->save()){
56 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
57 | return redirect('maintenance/city')->with('successTip','城市“'.$request->input('City.name').'”修改成功');
58 | }
59 | }
60 |
61 | public function destroy($id)
62 | {
63 | if(!Gate::allows('城市-删除')){ return redirect(url('/')); }
64 | $city=City::find($id);
65 | $this->log(__METHOD__,__FUNCTION__,json_encode($city),Auth::user()['id']);
66 | $result=$city->delete();
67 | return ['success'=>$result];
68 | }
69 |
70 | public function validatorCities(Request $request){
71 | $validator=Validator::make($request->input(),[
72 | 'City.name'=>'required|max:50',
73 | ],[
74 | 'required'=>':attribute 为必填项',
75 | ],[
76 | 'City.name'=>'城市名称',
77 | ]);
78 | return $validator;
79 | }
80 | }