| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 85.71% covered (warning) | 85.71% | 6 / 7 | CRAP | 97.83% covered (success) | 97.83% | 45 / 46
WaybillPriceModelsController | 0.00% covered (danger) | 0.00% | 0 / 1 | 85.71% covered (warning) | 85.71% | 6 / 7 | 14 | 97.83% covered (success) | 97.83% | 45 / 46
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% | 5 / 5
store | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 6 / 6
edit | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 6 / 6
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 3.02 | 87.50% covered (warning) | 87.50% | 7 / 8
destroy | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 5 / 5
validateWaybillPriceModel | 100.00% covered (success) | 100.00% | 1 / 1 | 1 | 100.00% covered (success) | 100.00% | 13 / 13

1 | <?php
2 |
3 | namespace App\Http\Controllers;
4 |
5 | use App\WaybillPriceModel;
6 | use App\Carrier;
7 | use App\Province;
8 | use App\Unit;
9 | use Illuminate\Http\Request;
10 | use Illuminate\Support\Facades\Auth;
11 | use Illuminate\Support\Facades\Gate;
12 | use Illuminate\Support\Facades\Validator;
13 |
14 | class WaybillPriceModelsController extends Controller
15 | {
16 | public function index()
17 | {
18 | if(!Gate::allows('计费模型-查询')){ return redirect(url('/')); }
19 | $waybillPriceModels= WaybillPriceModel::paginate(50);
20 | return view('waybill.waybillPriceModel.index',['waybillPriceModels'=>$waybillPriceModels]);
21 | }
22 |
23 |
24 | public function create()
25 | {
26 | if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); }
27 | $carriers=Carrier::get();
28 | $provinces=Province::with('cities')->get();
29 | $units=Unit::get();
30 | return view('waybill.waybillPriceModel.create',['carriers'=>$carriers,'provinces'=>$provinces,'units'=>$units]);
31 | }
32 |
33 |
34 | public function store(Request $request)
35 | {
36 | if(!Gate::allows('计费模型-录入')){ return redirect(url('/')); }
37 | $this->validateWaybillPriceModel($request)->validate();
38 | $waybillPriceModel=$request->input('WaybillPriceModel');
39 | WaybillPriceModel::create($waybillPriceModel);
40 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
41 | return redirect('waybillPriceModel')->with('successTip','新计费模型录入成功');
42 | }
43 |
44 |
45 |
46 | public function edit($id)
47 | {
48 | if(!Gate::allows('计费模型-编辑')){ return redirect(url('/')); }
49 | $waybillPriceModel=WaybillPriceModel::find($id);
50 | $carriers=Carrier::get();
51 | $provinces=Province::with('cities')->get();
52 | $units=Unit::get();
53 | return view('waybill.waybillPriceModel.edit',['waybillPriceModel'=>$waybillPriceModel,'carriers'=>$carriers,'provinces'=>$provinces,'units'=>$units]);
54 | }
55 |
56 |
57 | public function update(Request $request, $id)
58 | {
59 | if(!Gate::allows('计费模型-编辑')){ return redirect(url('/')); }
60 | $this->validateWaybillPriceModel($request)->validate();
61 | $waybillPriceModel=WaybillPriceModel::find($id);
62 | $waybillPriceModel->fill($request->input('WaybillPriceModel'));
63 | if ($waybillPriceModel->save()){
64 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
65 | return redirect('waybillPriceModel')->with('successTip','新计费模型修改成功');
66 | }
67 | }
68 |
69 | public function destroy($id)
70 | {
71 | if(!Gate::allows('计费模型-删除')){ return redirect(url('/')); }
72 | $waybillPriceModel=WaybillPriceModel::find($id);
73 | $this->log(__METHOD__,__FUNCTION__,json_encode($waybillPriceModel),Auth::user()['id']);
74 | $result=$waybillPriceModel->delete();
75 | return ['success'=>$result];
76 | }
77 |
78 | protected function validateWaybillPriceModel(Request $request){
79 | $min = $request->input('WaybillPriceModel.range_min');
80 | $validator= Validator::make($request->input(),[
81 | 'WaybillPriceModel.province_id'=>'required|integer',
82 | 'WaybillPriceModel.carrier_id'=>'required|integer',
83 | 'WaybillPriceModel.unit_id'=>'required|integer',
84 | 'WaybillPriceModel.city_id'=>'required|Integer',
85 | 'WaybillPriceModel.range_min'=> 'required|min:0|numeric',
86 | 'WaybillPriceModel.range_max'=> "required|min:{$min}|numeric",
87 | 'WaybillPriceModel.unit_price'=>'required|min:0|numeric',
88 | 'WaybillPriceModel.initial_weight'=>'required|min:0|numeric',
89 | ],[
90 | 'required'=>':attribute 为必填项',
91 | 'min' =>':attribute 数值过小',
92 | 'max' => ':attribute 数值过大',
93 | 'numeric' =>':attribute 应为数字',
94 | 'integer'=> ':attribute 选择错误',
95 | ],[
96 | 'WaybillPriceModel.province_id'=>'省份',
97 | 'WaybillPriceModel.carrier_id'=>'承运商',
98 | 'WaybillPriceModel.unit_id'=>'计重单位',
99 | 'WaybillPriceModel.city_id'=>'城市',
100 | 'WaybillPriceModel.range_min'=>'价格区间最小值',
101 | 'WaybillPriceModel.range_max'=>'价格区间最大值',
102 | 'WaybillPriceModel.unit_price'=>'单价',
103 | 'WaybillPriceModel.initial_weight'=>'始重',
104 | ]);
105 | return $validator;
106 | }
107 | }