3

I am trying to load an image in Laravel. The image is loading in a header.blade.php file. It is working fine on other pages but when it comes to localhost:8000/edit/id, It just doesn't work. I am loading Image like this:

<a href="{!! URL::to('/') !!}">
   <img src="images/logo.png" />
</a>

My Route:

Route::get('edit/{id}', array('uses' => 'HomeController@edit'));

My Controller:

 public function edit($id) {
    $reg = Register::find($id);
    $user = User::where('email', Session::get('email'))->first();
    if ($reg->user_id == $user->id) {
        return View::make('edit')
                        ->with('id', $id);
    } else {
        return Redirect::to('dashboard')->with('wrong', 'Sorry! Something Went Wrong!');
    }
}

My image directory: public/images/logo.png

It is working fine on other pages like localhost:8000/about but it doesn't work on localhost/edit/2, any help?

3
  • 1
    try <img src="/images/logo.png" /> Commented Aug 4, 2015 at 9:31
  • Holy God! I spent last half an hour figuring it out, and just added a slash, like you said, and its working, Thanks! Commented Aug 4, 2015 at 9:34
  • Awesome! I've added my answer as an actual answer aswel. Feel free to mark it if it helped Commented Aug 4, 2015 at 9:37

3 Answers 3

9

The problem is that when you are on your url, it is not looking for the image in the root folder of your application, but in the current location.

So when you are visiting localhost/edit/2, it is looking for the image in localhost/edit, instead of in localhost.

Try changing your code by adding a slash in the beginning of the image

<a href="{!! URL::to('/') !!}">
   <img src="images/logo.png" />
</a>

to

<a href="{!! URL::to('/') !!}">
   <img src="/images/logo.png" />
</a>

Additionally, you should actually try the laravel way, like this:

{{ HTML::image('images/logo.png', 'logo') }}
Sign up to request clarification or add additional context in comments.

Comments

3

try

{{ HTML::image('images/logo.png', 'logo') }}

output will be like

<img src="http://your.url/images/logo.png" alt="logo">

also you can check image path using inspect element in browser

1 Comment

You can also do <img src="<?= asset('images/logo.png') ?>" alt="logo">
1
<img src="public/storage/cover_images/image_name.format"  class="img-resposive">

it will load your picture otherwise check the path to image and previlages of the folders you can load image only from a public path due to security reasons

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.