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