【发布时间】:2017-10-25 09:29:42
【问题描述】:
我无法将图像上传到数据库,每次尝试上传时,发送到数据库的数据都是空的,但我的本地磁盘能够存储图像,我不知道该怎么办。我想我可能在控制器部分做错了什么
控制器:
public function store1(Request $request){
$this->validate($request, [
'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($request->hasFile('file')) {
$image = $request->file('file');
$name = $image->getClientOriginalName();
$size = $image->getClientSize();
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
$userImage = new UserImage;
$userImage->name = $name;
$userImage->size = $size;
//dd($userImage);
$userImage->save;
}
我想上传到的表:
Schema::create('user_images', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('size');
$table->integer('user_id');
$table->timestamps();
});
【问题讨论】:
-
在获得标志之前.. read this previous answer.另外,请将
binary字段远离用户表,因为它会在大多数情况下减慢查询速度。简而言之,blob 应该在另一个表中。 -
@BagusTesa tks 回复,我刚才忘了更新我的问题,这是我使用的旧问题,我刚刚编辑了问题,请看一下,tks 帮助
-
hmm,将我的图像上传到数据库中,确实令人困惑......之前你有
$table->binary('userImage');。现在,你尝试过@Miggy 的建议了吗?你的代码丢失了()。 -
@BagusTesa 很困惑,因为我看到很多帖子说将图像存储到数据库中不是一件好事,所以我将其更改为仅存储名称和大小
-
好吧,它并没有那么糟糕,只是,您需要考虑通过将 blob 扔到另一个表中来减轻表负担。所以对主表的查询对于mysql来说仍然很容易处理。
标签: php laravel image-uploading