2017-01-16 110 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

它需要从创建时移动的路径获取图像即使您也可以从商店方法更改路径以存储上传的图像。在上面的例子中你的图像将存储在公共/图像文件夹中 –