使用Laravel 5.4,我正在尝试设置一个表单,让用户输入食品名称和其图像。在尝试上传PNG或JPG图像时,我会收到以下“验证错误”:
“图像必须是图像。”
如果我删除图像的验证,我会收到“致命错误异常”:
“调用null上的getClientOriginalExtension()成员函数”
这是Intervention / Image库提供的帮助函数。这可能意味着图像根本没有上传。
表单如下:
我漏掉了什么?
<form action="/hq/foods" method="POST">
{{ csrf_field() }}
<input type="text" name="name">
<input type="file" name="image">
<button type="submit" class="btn btn-success ">
ADD FOOD ITEM
</button>
</form>
控制器
public function store(Request $request) {
$this->validate($request, [
'name' => 'required|min:2|max:255',
'image' => 'required|image'
]);
$food_category = FoodCategory::find($request->food_category_id);
$food = new Food;
$food->name = $request->name;
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('img/foods/' . $filename);
Image::make($image)->resize(800, 400)->save($location);
$food->image = $filename;
$food->save();
Session::flash('success',
'
<h4>Success!</h4>
<p>The food item has been added to the Menu.</p>
');
return back();
}
我漏掉了什么?