我做了一個函數,在該函數中我上傳了一些產品的圖片。當我遍歷所有產品時,我也想展示圖片。上傳圖片並在視圖中顯示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