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 插入“图像文件”
之后我检查了但原因不明白。
解决方案
试试这个它可以帮助你..
$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();
推荐阅读
- windows - 如何为每个生产 PC 客户端更新便携式软件
- postgresql - go,gorm - 如何将 jsonb 和 json[] 插入 postgresql
- javascript - Webpack React - 组件问题
- swiftui - 将关闭键盘添加到 ZStack 时,SwiftUI 选择器停止工作
- java - apache NiFi 1.13.2 ListenWebsocket 不工作
- javascript - 从去抖动函数返回一个值
- oracle - Oracle 进行一对一连接
- flutter-web - 上传到颤振网络时图像已损坏
- azure-data-factory - 时间范围内来自 REST API 的 ADF 请求
- django - 如何在 Django 视图中调用 API