首页 > 解决方案 > 我想在 Laravel 上发布图片

问题描述

所以我创建了一个可以像这样发布图像的表单,并在我检查视图时尝试处理图像

<form  action="{{ $action }}" enctype="multipart/form-data" method="post">
 <input type="file" name="imagefile" accept="image/*">
 <input type="text" name="content">
 <button tyoe="submit">OK</button>
</form>

用控制器处理取出图像并保存位置,同时观看其他人的博客

$post_data = $request->except('imagefile');
$imagefile = $request->file('imagefile');
$temp_path = $imagefile->store('public/temp');

$a = new Test;
$a->fill($request->all())->save();

我以为一切顺利,但出现了错误

无法将值 NULL 插入“图像文件”

之后我检查了但原因不明白。

标签: phplaravel

解决方案


试试这个它可以帮助你..

$post_data = $request->except('imagefile');
$imagefile = $request->file('imagefile');
$temp_path = $imagefile->store('public/temp');
$filename = $request->file('image');
$post_data->imagefile = $filename;

$a = new Test;
$a->fill($post_data)->save();

或者

$a = new Test;
$a->imagefile = $filename; // here imagefile equal to your db fild
$a->save();

推荐阅读