【问题标题】:Image not displaying in Laravel 4 Framework图像未在 Laravel 4 框架中显示
【发布时间】:2015-01-14 22:37:31
【问题描述】:

它显示图像 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>

后控制器.php

public function show($number)
{
    //
    $post = Post::find($number);


    return View::make('posts.show')->with('post', $post);
}

Mysql 工作台作为“posts”表。

我已经尝试过那些 {{{ $post->user->img_path }}} {{ $post->img_path }} {{ $post->user->img_path}} {{ $post->img_path }}

它不成功,它的显示破坏了图像。

【问题讨论】:

    标签: php mysql laravel blade


    【解决方案1】:

    我想这是因为存储在数据库中的图像文件名引用是相对的。您需要输入完整路径,因为如果您转到 http://example.com/posts/1,您的模板将在名为的目录中查找 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 添加到您的图片路径。

    【讨论】:

      【解决方案2】:

      使用asset helper

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

      这会生成一个包含您的完整域的绝对路径

      【讨论】:

        猜你喜欢
        • 2018-07-31
        • 2013-06-13
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        • 1970-01-01
        • 2014-06-21
        相关资源
        最近更新 更多