为什么不使用Laravel 5.6中的验证更新表值?

时间:2018-10-25 09:31:18

标签: php laravel laravel-5.6

在laravel 5.6应用程序中,我具有表名称作为车辆,然后需要在VehicleController更新函数中使用验证来更新一些表值,

 $this->validate($request, [
            'provincename'     => 'required|min:3',
            'districtname'    => 'required',
            'townname'    => 'required',
            'brandname'    => 'required',
            'modelname'    => 'required',
            'year'    => 'required',
            'condition'    => 'required',
            'milage'    => 'required',
            'data'    => 'required',
            'price'    => 'required',
            'telephone'    => 'required',
            'categoryname'    => 'required',
            'tramsmission'    => 'required',
            'fueltype'    => 'required',
            'enginecapacity'   => 'required'
         ]);

我的更新控制器就是这样,

 $vehicle = Vehicle::find($id);

        $vehicle->provincename = $request->input('provincename');
        $vehicle->districtname = $request->input('districtname');
        $vehicle->townname = $request->input('townname');
        $vehicle->brandname = $request->input('brandname');
        $vehicle->modelname = $request->input('modelname');
        $vehicle->modelyear = $request->input('year');
        $vehicle->condition = $request->input('condition');
        $vehicle->milage = $request->input('milage');
        $vehicle->detail = $request->input('data');
        $vehicle->price = $request->input('price');
        $vehicle->telephone = $request->input('telephone');
        $vehicle->categoryname =  $request->input('categoryname');
        $vehicle->transmission = $request->input('transmission');
        $vehicle->fueltype = $request->input('fueltype');
        $vehicle->enginecapacity = $request->input('enginecapacity');
        $vehicle->user_id = Auth::user()->id;

        $vehicle->save();

我的更新没问题。这是工作。但通过控制器验证不会更新。未经验证,它正在工作。如何解决呢?这是什么问题?

1 个答案:

答案 0 :(得分:0)

尝试这种验证格式:

$validatedData = $request->validate([
    'provincename'     => 'required|min:3',
    'districtname'    => 'required',
    'townname'    => 'required',
    'brandname'    => 'required',
    'modelname'    => 'required',
    'year'    => 'required',
    'condition'    => 'required',
    'milage'    => 'required',
    'data'    => 'required',
    'price'    => 'required',
    'telephone'    => 'required',
    'categoryname'    => 'required',
    'tramsmission'    => 'required',
    'fueltype'    => 'required',
    'enginecapacity'   => 'required'
]);

编辑

不要忘记使用验证器。在use Validator;之后添加namespace