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
CarTypesController
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
 validatorCarType
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
<?php
namespace App\Http\Controllers;
use App\CarType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
class CarTypesController extends Controller
{
    public function index()
    {
        if(!Gate::allows('车型-查询')){ return redirect(url('/'));  }
        $carTypes=CarType::paginate(10);
        return view('maintenance.carType.index',['carTypes'=>$carTypes]);
    }
    public function create()
    {
        if(!Gate::allows('车型-录入')){ return redirect(url('/'));  }
        return view('maintenance.carType.create');
    }
    public function store(Request $request)
    {
        if(!Gate::allows('车型-录入')){ return redirect(url('/'));  }
        $this->validatorCarType($request)->validate();
        $carTypes=$request->input('CarType');
        if(CarType::create($carTypes)){
            $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
            return redirect('maintenance/carType')->with('successTip','新车辆“'.$request->input('CarType.name').'”添加成功');
        };
    }
    public function edit($id)
    {
        if(!Gate::allows('车型-编辑')){ return redirect(url('/'));  }
        $carType=CarType::find($id);
        return view('maintenance.carType.edit',['carType'=>$carType]);
    }
    public function update(Request $request, $id)
    {
        if(!Gate::allows('车型-编辑')){ return redirect(url('/'));  }
        $this->validatorCarType($request)->validate();
        $data=$request->input('CarType');
        $carType=CarType::find($id);
        $carType->fill($data);
        if ($carType->save()){
            $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
            return redirect('maintenance/carType')->with('successTip','车型“'.$request->input('CarType.name').'”修改成功');
        }
    }
    public function destroy($id)
    {
        if(!Gate::allows('车型-删除')){ return redirect(url('/'));  }
        $carType=CarType::find($id);
        $this->log(__METHOD__,__FUNCTION__,json_encode($carType),Auth::user()['id']);
        $result=$carType->delete();
        return ['success'=>$result];
    }
    protected  function validatorCarType(Request $request){
        $validator=Validator::make($request->input(),[
            'CarType.name'=>'required|max:50',
            'CarType.model'=>'nullable|alpha_dash|max:50',
            'CarType.length'=>'required|numeric|min:0',
            'CarType.load'=>'nullable|numeric|min:0',
        ],[
            'required'=>':attribute 为必填项',
            'alpha_dash'=>':attribute 应为字母,数字,下划线',
            'max'=>':attribute 字符过多或数值过大',
            'min'=>':attribute 不得为负',
            'numeric'=>':attribute 应为数字',
        ],[
            'CarType.name'=>'车辆名称',
            'CarType.model'=>'车辆型号',
            'CarType.length'=>'车辆长度',
            'CarType.load'=>'车辆载重',
        ]);
        return $validator;
    }
}