如何在 Laravel 的模型中进行上传的文件验证


How to do Uploaded File Validations in Model in Laravel

这是我上一个问题的延续

我正在使用验证模型

我将通过这种方式将值发送到模型

$VehicleData = Input::all();
$validation  = Validator::make($VehicleData, VehicleModel::$rules);

并使用以下规则进行验证

public static $rules = array(
        'VehicleNumber' => 'required|unique:vehicle', 
        'NumberSeats' => 'required', 
);

但是现在我想验证将文件上传到的大小和扩展名

我试图在模型中做到这一点SetFooAttribute

public function setInsurancePhotoAttribute($InsurancePhoto)
{
    if($InsurancePhoto && Input::file('InsurancePhoto')->getSize() < '1000')
    {
    $this->attributes['InsurancePhoto'] = Input::get('VehicleCode') . '-InsurancePhoto.' . Input::file('InsurancePhoto')->getClientOriginalExtension();
    Input::file('InsurancePhoto')->move('assets/uploads/vehicle/', Input::get('VehicleCode') . '-InsurancePhoto.' . Input::file('InsurancePhoto')->getClientOriginalExtension());
    }
}

但我意识到这是一种不好的做法。因为它只会在验证之后调用,并且当它被调用时VehicleModel::create($VehicleData);

我不知道在哪里进行文件验证以及如何在模型中进行。

请建议我继续的方法。

模型中的

public static $rules = array(
    'VehicleNumber'  => 'required|unique:vehicle', 
    'NumberSeats'    => 'required',
    'InsurancePhoto' => 'required|image|mimes:jpg,png,gif|max:2000'
);

在控制器中

$VehicleData = Input::all();
$validation  = Validator::make($VehicleData, VehicleModel::$rules);
    if($validation->passes())
    {
        // Input::file('InsurancePhoto') stuff
        // store the data to the db
    }