我有一个模态表单,可以将文件提交给控制器。我使用Validate facade进行验证。 EVerytime我上传文件它说没有文件(我的验证失败响应)。这段代码完美无缺,我无法弄清楚导致此问题的原因
<form enctype ="multipart/form-data" action = "/profile_image" method = "POST">
<label>Your Profile Image</label>
<input type = "file" name = "avatar">
{{ csrf_field() }}
<input type = "submit" class= "pull-right btn btn-sm btn-primary">Upload image</input>
</form>
这是我的控制器
public function userImage(Request $request){
$post = $request->all();
$val = \Validator::make($request->all(),
['avatar' => 'required']);
if ($val->fails()) {
echo "no file";
} else{
if($request->hasFile('avatar')) {
$avatar = $request->file('avatar');
$filename = time().'.'.$avatar->getClientOriginalExtension();
Image::make($avatar)->save('resources/assets/avatars',$filename);
$user=\Auth::user();
$user->avatar=$filename;
$user->save();
} else {
echo "No file found";
}
}
}
我得到了这个回复
if ($val->fails()) {
echo "no file";
}
我不明白是什么让验证者失败