| 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
UnitsController | 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
validatorUnit | 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\Unit;
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 UnitsController extends Controller
12 | {
13 |
14 | public function index()
15 | {
16 | if(!Gate::allows('计量单位-查询')){ return redirect(url('/')); }
17 | $units=Unit::paginate(10);
18 | return view('maintenance.unit.index',['units'=>$units]);
19 | }
20 |
21 | public function create()
22 | {
23 | if(!Gate::allows('计量单位-录入')){ return redirect(url('/')); }
24 | return view('maintenance.unit.create');
25 | }
26 |
27 |
28 | public function store(Request $request)
29 | {
30 | if(!Gate::allows('计量单位-录入')){ return redirect(url('/')); }
31 | $this->validatorUnit($request)->validate();
32 | $units=$request->input('Unit');
33 | if(Unit::create($units)){
34 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
35 | return redirect('maintenance/unit')->with('successTip','新计量单位“'.$request->input('Unit.name').'”添加成功');
36 | };
37 | }
38 |
39 | public function edit($id)
40 | {
41 | if(!Gate::allows('计量单位-编辑')){ return redirect(url('/')); }
42 | $unit=Unit::find($id);
43 | return view('maintenance.unit.edit',['unit'=>$unit]);
44 | }
45 |
46 | public function update(Request $request, $id)
47 | {
48 | if(!Gate::allows('计量单位-编辑')){ return redirect(url('/')); }
49 | $this->validatorUnit($request)->validate();
50 | $data=$request->input('Unit');
51 | $unit=Unit::find($id);
52 | $unit->fill($data);
53 | if ($unit->save()){
54 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
55 | return redirect('maintenance/unit')->with('successTip','计量单位“'.$request->input('Unit.name').'”修改成功');
56 | }
57 | }
58 |
59 | public function destroy($id)
60 | {
61 | if(!Gate::allows('计量单位-删除')){ return redirect(url('/')); }
62 | $unit=Unit::find($id);
63 | $this->log(__METHOD__,__FUNCTION__,json_encode($unit),Auth::user()['id']);
64 | $result=$unit->delete();
65 | return ['success'=>$result];
66 | }
67 |
68 | protected function validatorUnit(Request $request){
69 | $validator=Validator::make($request->input(),[
70 | 'Unit.name'=>'required|max:10',
71 | ],[
72 | 'required'=>':attribute 为必填项',
73 | 'max'=>':attribute 过长'
74 | ],[
75 | 'Unit.name'=>'计量单位名称',
76 | ]);
77 | return $validator;
78 | }
79 | }