图像未在Laravel 4框架中显示


Image not displaying in Laravel 4 Framework

它显示图像索引.blade.php https://i.stack.imgur.com/oS3zE.jpg 当我点击{{link_to_action(PostController@show','阅读博客'}}它拒绝显示图像的下一页时,它很好。 即使我签入元素检查 img 文件以打开 src="img/1.img' 也没有可用的图像。 当我回到索引.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>

显示刀片.php https://i.stack.imgur.com/34Wsh.jpg

    <div class="container">

    @if ($post->img_path)
        <img class="img-upload" src="{{ $post->img_path }}" style="max-width: 25%">
    @endif
    </div>

后控制器.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。如果你的图像只是存储在公共/img中,那么你可以在show.blade.php模板中做这样的事情:

@if ($post->img_path)
    <img class="img-upload" src="{{ asset($post->img_path) }}" style="max-width: 25%">
@endif

这会将网站的根 URL 附加到图像的路径前面。

使用asset帮助程序

{{ asset($post->img_path) }}

这将生成包含完整域的绝对路径