【发布时间】:2019-03-12 01:48:53
【问题描述】:
我做了一个上传一些产品图片的功能。当我遍历所有产品时,我也想显示图像。
问题是,图像正在上传,但是当我使用@foreach 迭代时无法呈现它。我尝试在控制器中获取路径,但没有成功。
到目前为止,我已经这样做了:
在 config/filesystems.php 我创建了一个自定义存储
'products' => [
'driver' => 'local',
'root' => public_path('/pictures/uploads/products'),
],
当我创建新产品时:
<h1>Create new product</h1>
{!! Form::open(['route' => 'vendor.allproducts', 'files'=>true]) !!}
<div class="form-group">
{!! Form::label('Title', 'Title:') !!}
{!! Form::text('title',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('File', 'File:') !!}
{!! Form::file('image') !!}
</div>
当我遍历我的产品信息时:
@foreach ($products as $product)
<tr>
.
.
<td><img src="{{ $product->imagePath }}"></td>
</tr>
@endforeach
在控制器中:
public function store(){
$product = Request::all();
$file = Request::file('image');
$extension = $file->getClientOriginalExtension();
Storage::disk('products')->put($file->getFilename().'.'.$extension, File::get($file));
//$product->imagePath = Input::file('image')->getRealPath();
Auth::user()->products()->create($product);
return redirect()->route('allproducts');
}
【问题讨论】:
-
为什么这个
//$product->imagePath = Input::file('image')->getRealPath();被评论了? -
因为它不起作用。在 $product->imagePath 列中获取文件路径的尝试失败。
标签: php laravel file-upload laravel-5 image-uploading