上传图片并在视图中显示Laravel 5.2 [英] Upload images and showing them in a view Laravel 5.2
本文介绍了上传图片并在视图中显示Laravel 5.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题是,图片正在上传,但是我不能用 @foreach
。
到目前为止,我已经完成了这个工作:
在config / filesystems.php中,我创建了一个自定义存储区
'products'=> [
'driver'=> 'local',
'root'=> public_path('/ pictures / uploads / products'),
],
新产品:
< h1>创建新产品< / 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');
解决方案
/ b>
if(input :: hasfile(image)){
$ files = Input :: file('image );
$ name = time()。_。 $文件 - > getClientOriginalName();
$ image = $ files-> move(public_path()。'/ image',$ name);
$ imagefile-> image = $ name;
}
$ imagefile-> save();
而您的视图应该是
< td>< img src ={{URL :: to('/ image /'。$ product-> image)}}>< / td>
I made a function in which I upload images of some products. When I iterate through all my products, I want to show the image too.
The problem is, the image is being uploaded, but I can't present it when i iterate with @foreach
. I tried in the controller to get the path but it didn't work.
So far I have done this:
In config/filesystems.php I created a custom storage
'products' => [
'driver' => 'local',
'root' => public_path('/pictures/uploads/products'),
],
When I create a new product:
<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>
When i iterate through my products info:
@foreach ($products as $product)
<tr>
.
.
<td><img src="{{ $product->imagePath }}"></td>
</tr>
@endforeach
And in Controller:
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');
}
解决方案
Your Store method will be
if(input::hasfile("image")){
$files = Input::file('image');
$name = time()."_". $files->getClientOriginalName();
$image = $files->move(public_path().'/image' , $name);
$imagefile->image=$name;
}
$imagefile->save();
And your view should be as
<td><img src="{{URL::to('/image/' . $product->image)}}"></td>
这篇关于上传图片并在视图中显示Laravel 5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文