php Laravel 显示数据库中的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28563018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Displaying image from database
提问by DNM
so i wanted to display the image not the image name in my views.
所以我想在我的视图中显示图像而不是图像名称。
i got this in my views
我认为这是我的观点
<img src="{{$post->image}}" />
what i want is to display the actual image not the name of the image. how can i achieve this? should i save the image first in the public folder? or my method is wrong? Enlighten me please? thank you so much. i have tried this method
我想要的是显示实际图像而不是图像的名称。我怎样才能做到这一点?我应该先将图像保存在公共文件夹中吗?还是我的方法不对?请赐教?太感谢了。我试过这个方法
{{ HTML::image($post->image, '', array('class' => 'image')); }}
but i still get the same output.
但我仍然得到相同的输出。
回答by lukasgeiter
First, you have to store the images in the public directory (if not the browser can't access them)
首先,您必须将图像存储在公共目录中(否则浏览器无法访问它们)
Then it depends what $post->image
actually is. If it is a path relative to public
you can do this:
然后这取决于$post->image
实际情况。如果它是相对于public
您的路径,则可以执行以下操作:
<img src="{{ asset($post->image) }}" />
Or:
或者:
{{ HTML::image($post->image, '', array('class' => 'image')); }}
Edit
编辑
Since you're images are stored in public/img
and the image
attribute only holds the name (without img/
) you have to prepend that:
由于您的图像存储在public/img
并且该image
属性仅包含名称(没有img/
),因此您必须预先添加:
<img src="{{ asset('img/' . $post->image) }}" />
回答by Koluchaw
You can use like this <img src="{{ asset('img')}}/{{ $post->image) }}" />
你可以这样使用 <img src="{{ asset('img')}}/{{ $post->image) }}" />
Your image directory is public/img/image's name
你的图片目录是 public/img/image 的名字
回答by Dom58
The image was saved to the database, but you don't know which folder on disk the image was saved to, so look on your controller to see where it is saved.
图像已保存到数据库中,但您不知道图像保存在磁盘上的哪个文件夹中,因此请查看控制器以查看保存位置。
Then in blade you can use:
然后在刀片中,您可以使用:
<img src="/folder_name_path/{{$post ->image}}" />
回答by ved prakash
Insert Your Image:
public function store(Request $request) { $pages = new Appsetting(); $pages->title = $request->input('title'); $pages->description = $request->input('description'); if ($request->hasfile('image')) { $file = $request->file('image'); $extension = $file->getClientOriginalExtension(); // getting image extension $filename = time() . '.' . $extension; $file->move('uploads/appsetting/', $filename); //see above line.. path is set.(uploads/appsetting/..)->which goes to public->then create //a folder->upload and appsetting, and it wil store the images in your file. $pages->image = $filename; } else { return $request; $pages->image = ''; } $pages->save(); return redirect('pagesurl')->with('success', 'App Setting Added'); }
Display it by index
public function index() { $pages = Appsetting::all(); return view('admin.appsettings.pages', compact('pages',$pages)); }
Come to Blade file. (
pages.blade.php
)@foreach ($pages as $page) <td> <img src="{{ asset('uploads/appsetting/' . $page->image) }}" /> </td> @endforeach
set your route and 100% working.
插入您的图片:
public function store(Request $request) { $pages = new Appsetting(); $pages->title = $request->input('title'); $pages->description = $request->input('description'); if ($request->hasfile('image')) { $file = $request->file('image'); $extension = $file->getClientOriginalExtension(); // getting image extension $filename = time() . '.' . $extension; $file->move('uploads/appsetting/', $filename); //see above line.. path is set.(uploads/appsetting/..)->which goes to public->then create //a folder->upload and appsetting, and it wil store the images in your file. $pages->image = $filename; } else { return $request; $pages->image = ''; } $pages->save(); return redirect('pagesurl')->with('success', 'App Setting Added'); }
按索引显示
public function index() { $pages = Appsetting::all(); return view('admin.appsettings.pages', compact('pages',$pages)); }
来到 Blade 文件。(
pages.blade.php
)@foreach ($pages as $page) <td> <img src="{{ asset('uploads/appsetting/' . $page->image) }}" /> </td> @endforeach
设置您的路线并 100% 工作。
回答by minitechi
if your image is in public/images folder use below.otherwise use the path in project for that image in the public folder.concatenate with the image extension.
如果您的图像在 public/images 文件夹中,请使用下面的。否则在公共文件夹中使用该图像的项目路径。与图像扩展名连接。
<img src="images/{{$post->image}}.jpg" alt={{$post->image}}/>
<img src="images/{{$post->image}}.jpg" alt={{$post->image}}/>