我想在Laravel上发布图片

时间:2019-11-29 02:30:24

标签: php 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插入'imagefile'

此后我检查了一下,但原因不明。

3 个答案:

答案 0 :(得分:2)

尝试一下可能会对您有所帮助。

$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();

答案 1 :(得分:1)

保存之前,您需要将文件名分配给values数组。也不要使用$request->all();,它也包含图像对象,并且在保存时不需要它。我们只需要文件名。在下面的代码中,我在保存文件之前将文件名分配到$ post_data数组中。

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

$filename = $request->file('image')->hashName();
$post_data->imagefile = $filename;

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

答案 2 :(得分:0)

请尝试在下面的行中获取有关图像的所有参数。

$ imagefile = $ _FILES;

在继续操作之前打印$ imagafile数组。