我做了一个函数,在该函数中我上传了一些产品的图片。当我遍历所有产品时,我也想展示图片。上传图片并在视图中显示Laravel 5.2
问题是,图像正在上传,但我无法呈现它,当我迭代@foreach
。我试图在控制器中获取路径,但没有奏效。
到目前为止,我已经做到了这一点:
在配置/ 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');
}
这是为什么'// $产品 - >的ImagePath =输入:: file('image') - > getRealPath();'注释? –
因为它不起作用。这是一个尝试在$ product-> imagePath列中获取文件路径的失败尝试。 – daino92