1

I am unable to fetch the correct image URL to display in view using the asset. The generated URL required storage which is missing in it when I use the asset function and I have to manually add storage in the path like asset('storage/' . $post->image)

I don't understand why the Laravel doesn't add storage automatically?

symlink storage

I have created a symlink of storage folder using the following command

php artisan storage:link

Question:
What I am looking for is the storage folder should be dynamically added to the path so I have to pass only $post->image.

Controller

public function store(PostRequest $request)
{
    // upload the image to storage
    $image = $request->image->store('posts', 'public');

    // create the post
    Post::create([
        'title'       => $request->title,
        'description' => $request->description,
        'content'     => $request->content,
        'image'       => $image,
    ]);

    // redirect with flash message
    return redirect(route('posts.index'))->with('success', 'Post is created');

}

DB image Column Stored Path

posts/ibexiCvUvbPKxzOLSMHQKPpDq7eZXrFA0stBoPfw.jpeg

View

<tbody>
@foreach($posts as $post)
    <tr>
        <td>
            <img src="{{asset($post->image)}}"  width="60" height="60" alt="">
        </td>
        <td>{{ $post->title }}</td>
    </tr>
@endforeach
</tbody>

Storage Path

enter image description here

HTML Source

enter image description here

As you can see in the above references to get the correct URL I have to add storage into asset('storage/'. $post->image)

Community
  • 1
  • 1
Code Lover
  • 8,099
  • 20
  • 84
  • 154
  • 1
    Hope this this works for you -https://stackoverflow.com/questions/50185273/laravel-5-4-storage-link – mightyteja Apr 02 '20 at 06:49
  • try /storage/posts/, it should work – Akshay Khale Apr 02 '20 at 06:50
  • This link might be helpful: https://stackoverflow.com/questions/30191330/laravel-5-how-to-access-image-uploaded-in-storage-within-view – Akshay Khale Apr 02 '20 at 06:50
  • @mightyteja this I know and already have a symlinked storage folder. My question is how can I avoid adding `storage` folder in the path so I can get the image directly using the table column `$post->image`. Please read the last line in question. – Code Lover Apr 02 '20 at 07:02
  • You can create your custom helper to use instead `asset`. Take a look at https://stackoverflow.com/questions/43493148/change-the-asset-helper-url-in-laravel – porloscerros Ψ Apr 02 '20 at 07:13

2 Answers2

4

use Storage::url() function

<tbody>
@foreach($posts as $post)
    <tr>
        <td>
            <img src="{{ Storage::url($post->image)}}"  width="60" height="60" alt="">
        </td>
        <td>{{ $post->title }}</td>
    </tr>
@endforeach
</tbody>

ref link https://laravel.com/docs/7.x/filesystem#file-urls

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33
0

another way that can be helpful is using ASSET_URL in your .env file such as:

APP_URL=http://localhost:8000
ASSET_URL = http://localhost:8000/storage

I appended 'storage' on my APP_URL to change default patch of asset helper function, then you can use asset function into your view such as this:

 <img src="{{asset($post->image)}}"  width="60" height="60" alt="">

I hope this way can help you.