Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

using Laravel wont show a image after link_to_action.

it showing the images index.blade.php http://imgur.com/hZshMrl 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 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 workbench http://imgur.com/sHHyhnv 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

Hi Brian,

In your show.blade.php try to echo out $post object

{{ $post }}

this should return your object in Json format.

Please paste what you've got here.

1 Answer

I found the answer for this

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.