Ironheartbj18
Ironheartbj18

Reputation: 85

Image not displaying in Laravel 4 Framework

it showing the images index.blade.php https://i.sstatic.net/oS3zE.jpg is fine when I am click on {{link_to_action(PostController@show', 'Read Blog'}} the next page it refused show a image. even I check in element inspect the img file to open the src="img/1.img' there is no image available. when I go to back index.blade.php its back to normal. why is that?

<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 https://i.sstatic.net/34Wsh.jpg

    <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 workbench as for "posts" table.

I have tried those {{{ $post->user->img_path }}} {{ $post->img_path }} {{ $post->user->img_path}} {{ $post->img_path }}

its not successful its showing broken the image.

Upvotes: 1

Views: 1415

Answers (2)

Martin Bean
Martin Bean

Reputation: 39429

I imagine it’s because the image filename reference stored in the database is relative. You’ll need to put the full path, because if you go to http://example.com/posts/1, your template is then going to look for img/filename.jpg in a directory called posts, which doesn’t exist.

Instead, you should use an absolute URL. If your images are just stored in public/img then you can do something like this in your show.blade.php template:

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

This will prepend the site’s root URL to your image’s path.

Upvotes: 1

lukasgeiter
lukasgeiter

Reputation: 153130

Use the asset helper

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

This generates an absolute path with your full domain

Upvotes: 0

Related Questions