2017-01-16 92 views
0

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

這是爲什麼'// $產品 - >的ImagePath =輸入:: file('image') - > getRealPath();'註釋? –

+0

因爲它不起作用。這是一個嘗試在$ product-> imagePath列中獲取文件路徑的失敗嘗試。 – daino92

回答

1

您的店鋪方法將

if(input::hasfile("image")){ 
     $files = Input::file('image'); 
     $name = time()."_". $files->getClientOriginalName(); 
     $image = $files->move(public_path().'/image' , $name); 
     $imagefile->image=$name; 
    } 
     $imagefile->save(); 

而且您認爲應儘可能

<td><img src="{{URL::to('/image/' . $product->image)}}"></td> 
+0

感謝您的回答,並很抱歉沒有看到它!它與我所做的不同,但它的工作原理! @recovery男人你能告訴我一種方法來修復變量中的文件的路徑,所以當我寫' daino92

+0

它需要從創建時移動的路徑獲取圖像即使您也可以從商店方法更改路徑以存儲上傳的圖像。在上面的例子中你的圖像將存儲在公共/圖像文件夾中 –