上传图像并在Laravel 5.2视图中显示它们

问题描述 投票:0回答:1

我做了一个功能,我上传了一些产品的图像。当我遍历我的所有产品时,我也希望显示图像。

问题是,图像正在上传,但是当我使用@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');
}
php laravel file-upload laravel-5 image-uploading
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.