Laravel上传多个文件 [英] Laravel upload multiple files
问题描述
我正在建立具有多个文件功能的画廊.
I'm building a gallery with multiple files functionality.
到目前为止,我遇到了两个问题,但让我们先粘贴我的代码.
So far I'm having two issues, but let's paste my code first.
控制器:
public function store(Request $request)
{
$gallery = new GalleryImage();
if($request->hasFile('image')) {
$images = $request->file('image');
foreach($images as $image) {
$path = $image->getClientOriginalName();
$name = time() . '-' . $path;
$gallery->image = $image->move(public_path().'/uploads/temp/', $name);
//$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);
}
}
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
return back()->with('success_message','Images has been uploaded!');
}
查看刀片:
<form action="{{ route('gallery-images.store') }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<input type="hidden" name="gallery_id" value="{{ $gallery->id }}">
<input type="file" name="image[]" id="id_imageGallery" class="form-control" multiple="multiple" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
当我有这样的代码并上传三个文件时,这是我的第一个问题,它已成功将文件存储到/uploads/temp目录中,但是在我的数据库中,我只能看到上传的是一个图像,而不是三个.
My first issue when I have my code like this and upload three files, it's successfully storing the files into /uploads/temp directory, but in my database I can see there's only one image uploaded, instead of three.
我的第二个问题是,当我更改代码并使用注释的部分时(因为我要将这三个图像存储到存储中):
My second issue is when I change my code and use the commented part (because I want to store those three images into Storage):
$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);
我收到此错误:
Call to a member function storeAs() on array
如何将这些多张图像上传到存储"文件夹并将其全部记录到数据库中?
How can I upload those multiple images into the Storage folder and record them all into the database?
-编辑-我已经解决了我的第一个问题!
-- EDIT -- I've solved my first issue!
我只是简单地将save方法和其他细节放入循环中.我应该已经考虑过这个了:)
I've simply put the save method and other details inside the loop. I've should have thought about this before :)
这是我更新的代码:
public function store(Request $request)
{
if($request->hasFile('image')) {
$images = $request->file('image');
foreach($images as $image) {
$gallery = new GalleryImage();
$path = $image->getClientOriginalName();
$name = time() . '-' . $path;
$gallery->image = $image->move(public_path().'/uploads/temp/', $name);
//$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
}
}
return back()->with('success_message','Images has been uploaded!');
}
现在唯一要做的就是如何将它们存储到文件存储中,而不是public_path并避免该错误:
Now only thing to do is how to store them into File Storage, instead of the public_path and avoid that error:
Call to a member function storeAs() on array
推荐答案
我终于解决了问题并将其发布,因此对某些人可能有用.
I've finally solved the issues and I'm posting it, so it may be useful for some.
这是我的代码:
public function store(Request $request)
{
if($request->hasFile('image')) {
foreach($request->image as $image) {
$path = $image->getClientOriginalName();
$name = time() . '-' . $path;
$gallery = new GalleryImage();
$gallery->image = $image->storeAs('public/gallery-images', $name);
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
}
}
return back()->with('success_message','The images have been uploaded!');
}
这篇关于Laravel上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!