-1

Whenever I open an item the link would be

http://localhost/mywebsite/public/item/{$item_id}

the avatar will break. Below is the code I used

<img src="uploads/avatars/{{ Auth::user()->avatar }}">

The location of the image is C:\xampp\htdocs\mywebsite\public\uploads\avatars

John Doe
  • 83
  • 8

2 Answers2

1

I presume that as you are using relative URLs the translated path in browser will be http://localhost/mywebsite/public/item/{$item_id}/uploads/avatars/{{ Auth::user()->avatar }}

And that's not what you want

Try

<img src="http://localhost/mywebsite/public/uploads/avatars/{{ Auth::user()->avatar }}">

In production in will be just

<img src="/uploads/avatars/{{ Auth::user()->avatar }}">

Am not sure it will work on xampp though

So to say, I recommend using Laravel Valet or Homestead instead of Xampp, it cuts some problems like yours off.

Paul Smalling
  • 131
  • 1
  • 8
  • What is it that I want? – John Doe Sep 22 '17 at 13:49
  • @JohnDoe You'll want to use `public_path()` to construct the correct URL, maybe combined with the `asset()` helper. Both are in the docs. – ceejayoz Sep 22 '17 at 15:21
  • you just added a `/`. I also tried that one, didn't work. Thank you for your reply – John Doe Sep 22 '17 at 15:32
  • Adding a `/` won't work, because your URLs have `/mywebsite/public/` at the start. Read https://stackoverflow.com/questions/2005079/absolute-vs-relative-urls - `src="uploads` is relative to the current URL, including any folders. – ceejayoz Sep 22 '17 at 15:35
-2

in File model i created a function to get file encoded base64

public function getFile()
{
    $path = storage_path('app'). "/" . $this->path;
    $type = File::mimeType($path);

    $file = Storage::disk('local')->get("/". $this->path);

    return "data:image/png;base64,".base64_encode($file);
}

in blade You can use like that

 <img src = '$file->getFile()'>
BugraDayi
  • 507
  • 1
  • 4
  • 12