它显示图像的索引index.blade.php http://imgur.com/hZshMrl很好,当我单击{{link_to_action(PostController @ show','Read Blog'}}}的下一页时,它拒绝显示图像。我签入元素检查img文件以打开src =“ img / 1.img',没有可用的图像。当我返回index.blade.php时,它恢复正常,这是为什么呢?
<div class="container">
@forelse($posts as $post)
<div class="blogs">
<h3>{{{ $post->title }}}</h3>
<p class="info"> {{{ $post->content }}} </p>
<p class="info"> by: {{{ $post->user->first_name }}} {{{ $post->user->last_name }}} </p>
{{link_to_action('PostController@show','Read Blog', array($post->id))}}
<span class="posted" style="text-align:left">{{{ $post->created_at->format(Post::DATE_FORMAT)}}}</span>
@if ($post->img_path)
<p style="text-align:right"><img class="img-upload" src="{{ $post->img_path }}" style="max-width: 25%">
@endif
</div>
@empty
<p>No Blogs</p>
@endforelse
{{ $posts->links() }}
</div>
show.blade.php http://imgur.com/WdtkyAt
<div class="container">
@if ($post->img_path)
<img class="img-upload" src="{{ $post->img_path }}" style="max-width: 25%">
@endif
</div>
Postcontroller.php
public function show($number)
{
//
$post = Post::find($number);
return View::make('posts.show')->with('post', $post);
}
Mysql工作台为“帖子”表。
我已经尝试过那些{{{$ post-> user-> img_path}}} {{$ post-> img_path}} {{$ post-> user-> img_path}} {{$ post-> img_path}}
其失败显示了图像。
我想这是因为存储在数据库中的图像文件名引用是相对的。您需要输入完整路径,因为如果转到http://example.com/posts/1,则模板将在名为posts的目录中查找img / filename.jpg,而不会存在。
相反,您应该使用绝对URL。如果您的图像只是存储在public / img中,则可以在show.blade.php模板中执行以下操作:
@if ($post->img_path)
<img class="img-upload" src="{{ asset($post->img_path) }}" style="max-width: 25%">
@endif
这会将网站的根URL放在图像的路径之前。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句