上传图片并在视图中显示它们 Laravel 5.2
Upload images and showing them in a view Laravel 5.2
我做了一个功能,可以上传一些产品的图片。当我遍历所有产品时,我也想展示图像。
问题是,图片正在上传,但是当我用 @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');
}
您的 Store 方法将是
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>
我做了一个功能,可以上传一些产品的图片。当我遍历所有产品时,我也想展示图像。
问题是,图片正在上传,但是当我用 @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');
}
您的 Store 方法将是
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>